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 onsubmit="return true;" action="upload.action" method="post" enctype="multipart/form-data"> <input type="button" onclick="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));
相关推荐
我的Struts2多文件上传实例 博文链接:https://zmx.iteye.com/blog/461026
Struts2多个文件上传 多个文件上传分为List集合和数组,下面我们着重介绍一下list集合的上传。都大同小异。
struts2多文件上传显示进度 无插件
AjaxFileUpload Struts2 多文件上传
struts2 多文件上传下载 Java 文件上传下载 有界面,文件流实现上传下载(可多文件)
Struts2 文件上传下载 多文件 单文件 实例;支持单个或多个文件的上传下载,并且自定义上传文件的大小和类型!!!
uploadify+struts2多文件上传实例,解决了在firefox下的session问题,相信能帮助到你的
文件上传比较多,多文件上传少一点 文件下载很少的,看似简单,实则不然 网上的Struts2进行的文件下载一般都是单文件或者固定的文件,并没有(很少)实现随意文件的下载的例子 提供多文件上传,上传成功后,提供...
ext结合struts2.0实现文件上传,代码详细,加载项目即可以看到效果。