function addComponent() { var uploadHTML=document.createElement("input"); uploadHTML.name="fileList"; uploadHTML.type="file"; document.getElementById("fi").appendChild(uploadHTML); uploadHTML=document.createElement("br"); document.getElementById("fi").appendChild(uploadHTML); }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <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> </web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="globalMessages"/> <constant name="struts.i18n.encoding" value="GBK"/> <package name="upload" extends="struts-default"> <action name="upload" class="lee.UploadAction"> <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/pjpeg</param> </interceptor-ref> <interceptor-ref name="defaultStack"/> <param name="savePath">/upload</param> <result name="input">/upload.jsp</result> <result>/succ.jsp</result> </action> </package> </struts>
UploadAction
package lee;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.ServletActionContext;
import java.util.*;
import java.io.*;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport
{
private String title;
private List<File> upload;
private List<String> uploadContentType;
private List<String> uploadFileName;
private String savePath;
public void setSavePath(String value)
{
this.savePath = value;
}
private String getSavePath() throws Exception
{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setTitle(String title) {
this.title = title;
}
public void setUpload(List<File> upload) {
this.upload = upload;
}
public void setUploadContentType(List<String> uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(List<String> uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getTitle() {
return (this.title);
}
public List<File> getUpload() {
return (this.upload);
}
public List<String> getUploadContentType() {
return (this.uploadContentType);
}
public List<String> getUploadFileName() {
return (this.uploadFileName);
}
@Override
public String execute() throws Exception
{
List<File> files = getUpload();
for (int i = 0 ; i < files.size() ; i++)
{
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName().get(i));
FileInputStream fis = new FileInputStream(files.get(i));
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
}
return SUCCESS;
}
}
upload.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%> <%@taglib prefix="s" uri="/struts-tags"%> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> <title>使用List上传多个文件</title> <SCRIPT type="text/javascript"> function addComponent() { var uploadHTML=document.createElement("<input type='file' name='upload'/>"); document.getElementById("fi").appendChild(uploadHTML); uploadHTML=document.createElement("<p/>"); document.getElementById("fi").appendChild(uploadHTML); } </SCRIPT> </head> <body> <s:fielderror /> <form οnsubmit="return true;" action="upload.action" method="post" enctype="multipart/form-data"> <input type="button" οnclick="addComponent();" value="添加文件" /> <span id="fi"> <input type='file' name='upload' /> <p /> </span> <input type="submit" value="上传" /> </form> </body> </html>
设置文件 Mimetype:response.setContentType(new MimetypesFileTypeMap().getContentType(file));