zhuhichn 发表于 2013-1-15 02:51:19

struts2 中的多个文件上传

在struts2中,struts单个文件上传比较简单的,如何动态的实现多个文件的上传,即我们想传几个文件就可以上传几个文件。其实多文件的上传和单个文件上传没有什么太大的区别,就是增加一个list集合,代码如下:
package com.sport.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.util.List;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction2 extends ActionSupport {/** * 支持多文件上传 */private static final long serialVersionUID = 1L;/** * 上传的file的name值 */private List<File> file;/** * 上传的文件名字,自动获取 */private List<String> fileFileName;/** * 上传的文件类型,自动获取 */private List<String> fileContentType;public List<File> getFile() {return file;}public void setFile(List<File> file) {this.file = file;}public List<String> getFileFileName() {return fileFileName;}public void setFileFileName(List<String> fileFileName) {this.fileFileName = fileFileName;}public List<String> getFileContentType() {return fileContentType;}public void setFileContentType(List<String> fileContentType) {this.fileContentType = fileContentType;}@Override@SuppressWarnings("unchecked")public String execute() throws Exception {System.out.println(file.size());for(int i=0;i<file.size();i++){InputStream is = new FileInputStream(file.get(i));/** * 上传到逻辑路径不是物理路径 */String path=ServletActionContext.getRequest().getRealPath("/upload");System.out.println(path);File target = new File(path,this.getFileFileName().get(i));OutputStream os = new FileOutputStream(target);byte[] buffer = new byte;int count = 0;while((count=is.read(buffer))>0){os.write(buffer, 0, count);}is.close();os.close();}return SUCCESS;}@Overridepublic void validate() {System.out.println("----validate------");if(null==file){this.addActionError("上传文件不能为空");}}}
其余没什么变化 ,我们要做的就是用JavaScript实现动态添加文件上传框,镶嵌到网页代码中,代码如下:
<%@ 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><mce:script type="text/javascript"><!--function addMore(){      var td = document.getElementById("more");      var br = document.createElement("br");    var input = document.createElement("input");    var button = document.createElement("input");      input.type = "file";    input.name = "file";      button.type = "button";    button.value = "删除";      button.onclick = function(){            td.removeChild(br);      td.removeChild(input);      td.removeChild(button);      }      td.appendChild(br);    td.appendChild(input);    td.appendChild(button);      }// --></mce:script></head><body><table align="center" width="40%" border="1" >   <s:form action="upload3" theme="simple" method="post" enctype="multipart/form-data">   <tr>       <td width="20%">username:</td>       <td width="80%"><s:textfield name="username"></s:textfield></td>   </tr>   <tr>       <td>password:</td>       <td><s:password name="password"></s:password></td>   </tr>   <tr>       <td>file:</td>       <td id="more"><s:file name="file"></s:file><input type="button" value="继续上传"></td>   </tr>   <tr>       <td><s:submit value="提交"></s:submit></td>       <td>       <br><br></td>   </tr>         </s:form> </table></body></html>
struts.xml文件配置如下:
<action name="upload3" class="com.struts.UploadAction2">         <result name="success">/upload_success.jsp</result>         <result name="input">/upload3.jsp</result>         <interceptor-ref name="fileUpload">         <param name="maximumSize">409600</param><!-- 最大值 -->         <param name="allowedTypesSet">application/vnd.ms-powerpoint</param><!-- 上传文件类型可以再tomcat下conf目录的web.xml查看各种文件类型信息-->         </interceptor-ref>         <interceptor-ref name="defaultStack"></interceptor-ref>      </action>
页: [1]
查看完整版本: struts2 中的多个文件上传