Struts1 与 Struts2多文件上传的比较
STRUTS 1 部分
STRUTS 1 的文件上传定义了专门的文件处理接口FormFile。查看源码才知道,FormFile与File完全不是一回事,所以想将FormFile转为File再处理就行不通了。
第一次写上传时,我使用了common-upload组件进行文件上传,后来发现STRUTS在request中根本读不到文件。后来查了一些资料才发现Struts在Action处理逻辑之前已经对request中的表单数据进行了wrap。所以就只能老老实实的按STRUTS自己的文件上传方法写了。
多文件上传与单个文件上传有些不太一样,单文件上传只需要在Form类中给一个FormFile类型的属性,就能接受到页面提交的文件数据。多文件上传时,STRUTS在页面端将多个文件wrap成了一个数组,转给From类时又wrap成了List。所以我们在页面上使用数组形式对文件进行提交,在Form类中用List对文件进行接收。以下是相关的代码:
1.jsp页面代码
<html:file property = "uploadFile[0].file"/><html:file property = "uploadFile[1].file"/>
public class AccuseForm extends ActionForm {private List evidences; public AccuseForm(){evidences = new ArrayList();evidences.add(new UploadFile());} public UploadFile getUploadFile(int index){int size = evidences.size();if(index > size - 1){for(int i = 0; i < index - size + 1; i++){evidences.add(new UploadFile());}}return (UploadFile)evidences.get(index);}public List getEvidences() {return evidences;}public void setEvidences(List evidence) {this.evidences = evidence;}}public class UploadFile implements Serializable {private FormFile file;private String newName;public FormFile getFile(){//System.out.println("run in uploadFile.getFile()");return this.file;}public void setFile(FormFile file){this.file = file;}}
public class AccuseAction extends DispatchAction { public ActionForward save(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response){AccuseForm f = (AccuseForm)form; List evidences = f.getEvidences(); try{ String dir = Constants._UPLOAD_DIR; File fDir = new File(dir); if(!fDir.exists()) fDir.mkdirs(); for(int i = 0; i < evidences.size(); i++){ UploadFile uploadFile = (UploadFile)evidences.get(i); uploadFile = renameFile(uploadFile); if(uploadFile.getFile() == null){ System.out.println("file is null"); break; }else{ //System.out.println("filename:::" + file.getFileName()); //System.out.println("file size:::" + file.getFileSize()); //System.out.println(dir); uploadFile(uploadFile, dir); } } }catch(Exception e){ e.printStackTrace(); } private void uploadFile(UploadFile file, String dir) throws FileNotFoundException, IOException{InputStream in = file.getFile().getInputStream(); OutputStream out = new BufferedOutputStream( new FileOutputStream( dir + file.getNewName() )); final int _BUFFERSIZE = 8192; int bytesRead = 0; byte[] buffer = new byte[_BUFFERSIZE]; while((bytesRead = in.read(buffer, 0, _BUFFERSIZE)) != -1){ out.write(buffer, 0, bytesRead); } out.flush(); out.close(); in.close();}}
package org.xwood.struts.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import org.apache.struts2.ServletActionContext;public class DemoAction{private String title; private File[] upload; private String[] uploadContentType; private String[] uploadFileName; /*//接受依赖注入的属性 private String savePath;//接受依赖注入的方法 public void setSavePath(String value){ this.savePath = value; } */ private String getSavePath() throws Exception { return ServletActionContext.getServletContext().getRealPath("upload"); }public void setTitle(String title) {this.title = title; }public void setUpload(File[] upload) {this.upload = upload; }public void setUploadContentType(String[] uploadContentType) {this.uploadContentType = uploadContentType; }public void setUploadFileName(String[] uploadFileName) {this.uploadFileName = uploadFileName; }public String getTitle() {return (this.title); }public File[] getUpload() {return (this.upload); }public String[] getUploadContentType() {return (this.uploadContentType); }public String[] getUploadFileName() {return (this.uploadFileName); } public String upload() throws Exception{File[] files = getUpload();String path = getSavePath();for (int i = 0 ; i < files.length ; i++){//以服务器的文件保存地址和原文件名建立上传文件输出流FileOutputStream fos = new FileOutputStream(path + File.separator + getUploadFileName()[i]);FileInputStream fis = new FileInputStream(files[i]);byte[] buffer = new byte[1024];int len = 0;while ((len = fis.read(buffer)) > 0){fos.write(buffer , 0 , len);} fos.close();// 注意:流应当关闭。 fis.close();} return "success";}}
<action name = "demo_*" class = "org.xwood.struts.action.DemoAction"method = "{1}"><result name="success" type="dispatcher">/success.jsp</result><result name="input">index.jsp</result><interceptor-ref name="fileUpload"><param name="maximumSize">20000</param> <param name ="allowedTypes"> *</param> </interceptor-ref><interceptor-ref name="defaultStack"></interceptor-ref></action>
<filter><filter-name>struts-cleanup</filter-name><filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class></filter><filter-mapping><filter-name>struts-cleanup</filter-name><url-pattern>/*</url-pattern></filter-mapping>