单个文件上传:
FileAction
package com.file; import java.io.File; import java.util.List; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionSupport; public class FilesAction extends ActionSupport{ private static final long serialVersionUID = -5016873153441103539L; private List<File> uploads; private List<String> fileNames; private List<String> uploadContentTypes; private List<String> names; public void setNames(List<String> names) { this.names = names; } public java.util.List<String> getUploadFileName() { return fileNames; } public void setUploadFileName(java.util.List<String> fileNames) { this.fileNames = fileNames; } public java.util.List<File> getUpload() { return uploads; } public void setUpload(java.util.List<File> uploads) { this.uploads = uploads; } public void setUploadContentType(java.util.List<String> contentTypes) { this.uploadContentTypes = contentTypes; } public java.util.List<String> getUploadContentType() { return this.uploadContentTypes; } public String execute() throws Exception { for(String s:names){ System.out.println(s); } if (uploads != null) { for (int i = 0; i < uploads.size(); i++) { File file=uploads.get(i); String fileName=fileNames.get(i); String targPath="e:/upload"; File toFile=new File(targPath,fileName); FileUtils.copyFile(file,toFile); System.out.println("i==="+i); } } return SUCCESS; } }
jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <s:form action="fileAction" method="post" namespace="/file" enctype ="multipart/form-data"> <s:file name="doc" label="image File"></s:file> <s:submit></s:submit> </s:form> </body> </html>
多个文件上传
FilesAction
package com.file; import java.io.File; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; import javax.servlet.ServletContext; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FileUtils; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.ActionSupport; public class FileAction extends ActionSupport implements ServletRequestAware{ private static final long serialVersionUID = -5016873153441103539L; private File doc; private String fileName; private String contentType; private HttpServletRequest request; private String path=""; public String getPath() { return path; } public void setPath(String path) { this.path = path; } public void setDoc(File file) { this.doc = file; } public void setDocFileName(String fileName) { this.fileName = fileName; } public void setDocContentType(String contentType) { this.contentType = contentType; } @Override public void setServletRequest(HttpServletRequest arg0) { this.request=arg0; } public String execute() throws Exception { String targetDirectory="e:/upload"; String targetFileName =fileName; System.out.println(targetDirectory); File target = new File(targetDirectory, targetFileName); FileUtils.copyFile(doc, target); path=targetDirectory+"/"+targetFileName; return SUCCESS; } }
jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script language="javascript">
function addComponent() { var uploadHTML = document.createElement( "<input type='file' name='upload'/>"); document.getElementById("files").appendChild(uploadHTML); uploadHTML = document.createElement( "<p/>"); document.getElementById("files").appendChild(uploadHTML); } </script> </head> <body> <input type="button" onclick="addComponent();" value="添加文件" /> <br /> <s:form onsubmit="return true;" action="filesAction" namespace="/file" enctype="multipart/form-data"> <span id="files"> <input type='file' name='upload' /> <p/> </span> <s:textfield name="names"></s:textfield><br> <s:textfield name="names"></s:textfield><br> <s:textfield name="names"></s:textfield><br> <s:submit value="上传"></s:submit> </s:form> </body>
</html>