方案一:COS + UploadBean + FileMover(傳統經典)
所需 JAR
cos.jar:多部分請求解析
uploadbean.jar:上傳處理核心
filemover.jar:檔案改名/移動監聽器
上傳表單頁
<form name="form1" method="POST" action="upload.jsp" enctype="multipart/form-data">
標題: <input name="title" type="text" value="中文字">
附件: <input name="attach" type="file" id="attach" size="50">
<input name="ok" type="submit" value="提交">
</form>
處理頁 (upload.jsp) 關鍵代碼
<%@ page import="com.jspsmart.upload.*, javazoom.upload.*, uploadutilities.FileMover,
java.text.SimpleDateFormat, java.io.File, java.util.*" %>
<%
request.setCharacterEncoding("GBK"); // 統一編碼
FileMover fileMover = new FileMover();
UploadBean upBean = new UploadBean();
MultipartFormDataRequest mrequest = null;
Hashtable files = null;
if (MultipartFormDataRequest.isMultipartFormData(request)) {
mrequest = new MultipartFormDataRequest(request, null, 100*1024*1024,
MultipartFormDataRequest.COSPARSER, "GBK");
String title = new String(mrequest.getParameter("title").getBytes("ISO-8859-1"), "GBK");
files = mrequest.getFiles();
}
// 產生唯一檔名:時間戳 + 原副檔名
String localName = mrequest.getParameter("oldfilename");
int dot = localName.lastIndexOf(".");
String ext = localName.substring(dot);
String serverName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + ext;
String webRoot = request.getRealPath("/");
String saveDir = webRoot + "attach";
new File(saveDir).mkdirs();
upBean.setFolderstore(saveDir);
upBean.addUploadListener(fileMover);
fileMover.setNewfilename(serverName);
upBean.store(mrequest, "attach");
%>
方案二:Struts 1.1+ 內建 FormFile(框架整合)
JSP 頁面 (Struts 標籤庫)
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html>
<html:form action="/uploadAction.do" enctype="multipart/form-data">
<html:file property="theFile" />
<html:submit />
</html:form>
</html:html>
Form Bean
public class UploadForm extends ActionForm {
private FormFile theFile;
// getter/setter
}
Action 類
public class UploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UploadForm uf = (UploadForm) form;
FormFile file = uf.getTheFile();
String savePath = request.getRealPath("/") + "uploads/";
new File(savePath).mkdirs();
try (InputStream in = file.getInputStream();
FileOutputStream out = new FileOutputStream(savePath + file.getFileName())) {
byte[] buf = new byte[4096];
int len;
while ((len = in.read(buf)) != -1) out.write(buf, 0, len);
} catch (IOException e) { /* handle */ }
return mapping.findForward("success");
}
}
struts-config.xml 關鍵配置
<controller maxFileSize="100M" />
<form-bean name="uploadForm" type="com.example.UploadForm" />
<action path="/uploadAction" type="com.example.UploadAction"
name="uploadForm" scope="request" input="/upload.jsp">
<forward name="success" path="/success.jsp" />
</action>
方案對比與選型建議
| 維度 | COS/UploadBean | Struts FormFile |
| 依賴 | 三個獨立 JAR | 僅 Struts 核心 |
| 檔案改名 | FileMover 監聽器靈活控制 | 需自行在 Action 中處理 |
| 編碼處理 | 手動轉碼較繁瑣 | 配合 Filter + controller 統一 |
| 進度條/大檔 | 需自行擴充 | Struts 2 + Plugin 支援較好 |
| 適用場景 | 非 Struts 專案、舊系統維護 | Struts 專案首選 |
現代化替代方案
- Servlet 3.0+:
request.getPart("file")、@MultipartConfig 原生支援,零依賴
- Apache Commons FileUpload:Spring MVC、Struts 2 底層預設實作
- Spring MVC:
MultipartFile + MultipartResolver 簡潔優雅