一
< ?xml version=
< !DOCTYPE web
PUBLIC
< web
< servlet>
< servlet
< servlet
< init
< param
< param
< /init
< init
< param
< param
< /init
< load
< /servlet>
< !
< servlet
< servlet
< url
< /servlet
< !
< welcome
< welcome
< /welcome
< !
< error
< error
< location>/error
< /error
< /web
二
< ?xml version=
< !DOCTYPE struts
< struts
< !
< form
< form
< form
< form
< form
< form
< form
< form
< form
< form
< form
< form
< form
< form
< /form
< /form
< !
< global
< forward name=
< forward name=
< /global
< !
< action
< action path=
name=
< forward name=
< /action>
< /action
< /struts
三
< %@ page language=
< %@ taglib uri=
< %@ taglib uri=
< %@ taglib uri=
< %@ taglib uri=
< html>
< head>
< title>上傳多個文件的jsp文件< /title>
< meta http
< meta http
< meta http
< meta http
< meta http
< /head>
< body>
< html:form action=
< table border=
< tr bgColor=#
< td align=
< td align=
< td align=
< /tr>
< tr>
< td align=
< option value=
< option value=
< option value=
< option value=
< option value=
< /select>< /td>
< td align=
< td align=
< /td>
< /tr>
< tr>
< td align=
< option value=
< option value=
< option value=
< option value=
< option value=
< /select>< /td>
< td align=
< td align=
< /td>
< /tr>
< tr>
< td align=
< option value=
< option value=
< option value=
< option value=
< option value=
< /select>< /td>
< td align=
< td align=
< /td>
< /tr>
< tr>
< td align=
< option value=
< option value=
< option value=
< option value=
< option value=
< /select>< /td>
< td align=
< td align=
< /td>
< /tr>
< tr>
< td align=
< option value=
< option value=
< option value=
< option value=
< option value=
< /select>< /td>
< td align=
< td align=
< /td>
< /tr>
< tr>
< td align=
< option value=
< option value=
< option value=
< option value=
< option value=
< /select>< /td>
< td align=
< td align=
< /td>
< /tr>
< tr>
< td align=
< option value=
< option value=
< option value=
< option value=
< option value=
< /select>< /td>
< td align=
< td align=
< /td>
< /tr>
< tr>
< td align=
< option value=
< option value=
< option value=
< option value=
< option value=
< /select>< /td>
< td align=
< td align=
< /td>
< /tr>
< tr>
< td align=
< option value=
< option value=
< option value=
< option value=
< option value=
< /select>< /td>
< td align=
< td align=
< /td>
< /tr>
< tr>
< td align=
< option value=
< option value=
< option value=
< option value=
< option value=
< /select>< /td>
< td align=
< td align=
< /td>
< /tr>
< tr>
< td align=
< td align=
< td align=
onclick=
< /tr>
< /table>
< /html:form>
< /body>
< /html>
四
五
package com
/**
* @author anEngineer 處理上傳文件action
*/
public class UploadAction extends Action {
public ActionForward execute(
ActionMapping mapping
ActionForm form
HttpServletRequest request
DynaActionForm fileForm = (DynaActionForm) form;
String[] types = (String[]) fileForm
String[] names = (String[]) fileForm
FormFile file
FormFile file
FormFile file
FormFile file
FormFile file
FormFile file
FormFile file
FormFile file
FormFile file
FormFile file
Map< String
fileMap
fileMap
fileMap
fileMap
fileMap
fileMap
fileMap
fileMap
fileMap
fileMap
Set fileSet = fileMap
Iterator iter = erator();
// 取當前系統路徑E:\Tomcat
String filePath = this
// 保存文件的文件夾
File savePath = new File(filePath +
filePath = filePath+
if (!savePath
savePath
}
while (iter
Map
String key = (String) unit
FormFile file = (FormFile) unit
//文件大小符合要求
if ((file
//圖片類別
String photoType = key
//圖片描述
String photoName = key
//存數據庫操作
//
//判斷是否重名
if(DealPhoto
DealPhoto
try {
InputStream stream = file
// 建立一個上傳文件的輸出流
OutputStream bos = new FileOutputStream(filePath+file
int bytesRead =
byte[] buffer = new byte[
while ((bytesRead = stream
bos
}
bos
stream
} catch (Exception e) {
e
}
}
}
return mapping
//還有其他可以改正的地方
}
}
六
package com
/**
* 圖片處理
* @author anEngineer
*
*/
public class DealPhoto {
/**
* 判斷照片類型
* @param file
* @return
*/
public static boolean isPhoto(FormFile file) {
String fileName = getString(file
if (fileName
return false;
if ((fileName
|| (fileName
|| (fileName
return true;
else
return false;
}
/**
*
* @param str
* @return
*/
public static String getString(String str) {
if (str == null)
str =
if (str
str =
str = str
return str;
}
/**
* 判斷文件是否存在
* @param fileName
* @param dir
* @return
*/
public static boolean isFileExist(String fileName
File files = new File(dir + fileName);
return (files
}
/**
* 重命名
* @param fileName
* @param dir
*/
public static void rename(String fileName
String extendFile =
if (isJpg(fileName))
extendFile =
else if (isGif(fileName))
extendFile =
else if (isPng(fileName))
extendFile =
else
extendFile =
Random random = new Random();
int add = random
String ret = fileName + add + extendFile;
while (isFileExist(ret
add = random
ret = fileName + add + extendFile;
}
File file = new File(dir + fileName);
File reFile = new File(dir + ret);
file
}
public static boolean isGif(String file) {
if (file
return true;
} else {
return false;
}
}
public static boolean isJpg(String file) {
if (file
return true;
} else {
return false;
}
}
public static boolean isPng(String file) {
if (file
return true;
} else {
return false;
}
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/27880.html