struts2文件上传还是比较简单的,原来看起来好像是这么做的,他把Action中的一个File对象映射到上传上来的临时文件上,然后根据业务逻辑去判断是不是该保留这个文件,如果该保留就把它给写进正确的目录下。 package net.ynyee.webqq.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class FileUpload extends ActionSupport { private String savePath = null; public String getSavePath() { return savePath; } public void setSavePath(String savePath) { this.savePath = savePath; } private File[] upload = null; private String[] uploadFileName = null; private String[] uploadContentType = null; public String[] getUploadContentType() { return uploadContentType; } public void setUploadContentType(String[] uploadContentType) { this.uploadContentType = uploadContentType; } public File[] getUpload() { return upload; } public void setUpload(File[] upload) { this.upload = upload; } public String[] getUploadFileName() { return uploadFileName; } public void setUploadFileName(String[] uploadFileName) { this.uploadFileName = uploadFileName; } @Override public String execute() throws Exception { for (int i = 0; i < getUpload().length; i++) { File dir = new File(ServletActionContext.getServletContext() .getRealPath("/") + File.separator + getSavePath() + File.separator + "20000"); if (!dir.exists()) { dir.mkdirs(); } File file = new File(dir, System.currentTimeMillis() + this.getUploadFileName()[i]); if (!file.exists()) { file.createNewFile(); } FileOutputStream fos = new FileOutputStream(file); FileInputStream fis = new FileInputStream(getUpload()[i]); byte[] b = new byte[1024]; int len = 0; while ((len = fis.read(b)) > 0) { fos.write(b, 0, len); } fos.flush(); fos.close(); fis.close(); } return SUCCESS; } }