struts2多个文件上传下载

页面代码:

注意,我在页面在一个新建的文件夹下(TheNameSpace)

 

[html][/html] view plaincopy

  1. <%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8″%>
  2. <%@ taglib prefix=”s” uri=”/struts-tags” %>
  3. <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
  4. <html>
  5.   <head>
  6.     <title>My JSP ‘uplod.jsp’ starting page</title>
  7.   </head>
  8.   <body>
  9.   <h1>file upload</h1>
  10.   <s:form action=”uploadList.action” enctype=”multipart/form-data” theme=”simple”>
  11.   <table cellpadding=”0″  height=”5″ >
  12.   <tr>
  13.   <td>用户:</td>
  14.   <td>
  15.   <s:textfield name=”username”/>
  16.   </td>
  17.   </tr>
  18.   <tr>
  19.   <td>密码:</td>
  20.  <td> <s:password name=”passwrod”/></td>
  21.    </tr>
  22.    <tr >
  23.    <td>文件:</td>
  24.    <td id=”fileID”>
  25.    <s:fielderror name=”file”/>
  26.   <s:file name=”file” />
  27.   <a href=”javascript:addFiles()” >添加上传</a>
  28.   </td>
  29.   </tr>
  30.   <tr>
  31.   <td colspan=”2″>
  32.   <s:submit value=”提交”/>
  33.   <s:reset value=”重置”/>
  34.     </td>
  35.   </tr>
  36.    </table>
  37.   </s:form>
  38.   <script language=”javascript”>
  39.     function addFiles(){
  40.     var tdID=document.getElementById(“fileID”);
  41.     var br=document.createElement(“br”);
  42.     var file=document.createElement(“input”);
  43.     var button=document.createElement(“input”);
  44.     file.type=”file”;
  45.     file.name=”file”;
  46.     button.type=”button”;
  47.     button.value=”remove”;
  48.     button.onclick=function(){
  49.     tdID.removeChild(br);
  50.     tdID.removeChild(file);
  51.     tdID.removeChild(button);
  52.     }
  53.     tdID.appendChild(br);
  54.     tdID.appendChild(file);
  55.     tdID.appendChild(button);
  56.     }
  57.   </script>
  58.   </body>
  59. </html>

struts.xml配制文件:

 

 

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2.  <!DOCTYPE struts PUBLIC
  3.     “-//Apache Software Foundation//DTD Struts Configuration 2.3//EN”
  4.     “http://struts.apache.org/dtds/struts-2.3.dtd”>
  5.     <struts>
  6.     <!– 常量 上传文件 最大大小 –>
  7.     <constant name=”struts.multipart.maxSize” value=”20971520″/>
  8.     <!– 包括这个xml –>
  9.     <include file=”strtus_1.xml”/>
  10.     <!– 后台action  –>
  11.     <package name=”back” extends=”struts-default” namespace=”/TheNameSpace”>
  12.     <action name=”logins” class=”com.rui.struts2.SpaceLogin”>
  13.     <result name=”success”>showUser.jsp</result>
  14.     </action>
  15.     <action name=”uploadList” class=”com.rui.struts.UploadList”>
  16.     <result name=”success”>ok.jsp</result>
  17.     <result name=”input”>/${pageContext.request.contextPath}/TheNameSpace/uploadList.jsp</result>
  18.     <!–拦截上上传文件的 大小、格式  –>
  19.     <interceptor-ref name=”fileUpload”>
  20.     <param name=”setAllowedTypes”>image/pjpeg,image/gif,image/bmp,image/jpeg,image/jpg, text/plain, application/java-archive</param>
  21.     <param name=”maximumSize”>524288</param>
  22.     </interceptor-ref>
  23.      <interceptor-ref name=”basicStack”/>
  24.     </action>
  25.     <action name=”upload” class=”com.rui.struts.Upload”>
  26.     <result name=”success”>ok.jsp</result>
  27.     <result name=”input”>upload.jsp</result>
  28.     </action>
  29.     <action name=”download” class=”com.rui.struts.DownLoald”>
  30.     <result  type=”stream”>
  31.     <!– 要下载的文件
  32.     <param name=”contentDisposition”>attachment;filename=”bbbb.txt”</param>
  33.     –>
  34.     <!– 自动寻找方法 –>
  35.     <param name=”inputName”>downloadFile</param>
  36.     </result>
  37.     </action>
  38.     <action name=”showlist” class=”com.rui.struts.ShowFileList”>
  39.     <result>download.jsp</result>
  40.     </action>
  41.     </package>
  42.     </struts>

action 上传类的代码:

 

 

[html][/html] view plaincopy

  1. package com.rui.struts;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.util.List;
  8. import org.apache.struts2.ServletActionContext;
  9. import com.opensymphony.xwork2.ActionSupport;
  10. public class UploadList extends ActionSupport {
  11.     private String username;
  12.     private String passwrod;
  13.     private List<File> file;
  14.     private List<String> fileFileName;
  15.     private List<String> fileContentType;
  16.     public String getUsername() {
  17.         return username;
  18.     }
  19.     public void setUsername(String username) {
  20.         this.username = username;
  21.     }
  22.     public String getPasswrod() {
  23.         return passwrod;
  24.     }
  25.     public void setPasswrod(String passwrod) {
  26.         this.passwrod = passwrod;
  27.     }
  28.     public List<File> getFile() {
  29.         return file;
  30.     }
  31.     public void setFile(List<File> file) {
  32.         this.file = file;
  33.     }
  34.     public List<String> getFileFileName() {
  35.         return fileFileName;
  36.     }
  37.     public void setFileFileName(List<String> fileFileName) {
  38.         this.fileFileName = fileFileName;
  39.     }
  40.     public List<String> getFileContentType() {
  41.         return fileContentType;
  42.     }
  43.     public void setFileContentType(List<String> fileContentType) {
  44.         this.fileContentType = fileContentType;
  45.     }
  46.     @Override
  47.     public void validate() {
  48.         System.out.println(“执行了验证器…”);
  49.         if(null==file){
  50.             addFieldError(“file”, “请选择文件!”);
  51.         }
  52.     }
  53.     @Override
  54.     public String execute() throws Exception {
  55.         if(null==file||file.size()<=0){
  56.             addFieldError(“file”, “请上传符合格式的文件….”);
  57.             return INPUT;
  58.         }
  59.     //获得路么
  60.     String path=ServletActionContext.getRequest().getRealPath(“upload”);
  61.     for(int i=0;i<file.size();i++){
  62.     //输入流
  63.     InputStream is=new FileInputStream(file.get(i));
  64.     //File 对象
  65.     File fileObj=new File(path,fileFileName.get(i));
  66.     //输出流
  67.     OutputStream os=new FileOutputStream(fileObj);
  68.     byte [] by=new byte[400];
  69.     int length=0;
  70.     while(-1!=(length=is.read(by))){
  71.         os.write(by,0,length);
  72.     }
  73.     os.close();
  74.     is.close();
  75.     }
  76.         return SUCCESS;
  77.     }
  78. }

 

 

下载页面测试 :

 

[html][/html] view plaincopy

  1. <%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8″%>
  2. <%@ taglib prefix=”s” uri=”/struts-tags” %>
  3. <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
  4. <html>
  5.   <head>
  6.     <title>My JSP ‘download.jsp’ starting page</title>
  7.   </head>
  8.   <body>
  9.   <s:iterator value=”listName” var=”lname”>
  10.   <s:property value=”lname”/><br/><br/>
  11.   <a href=”download.action?lname=<s:property value=’#lname’/>” >下载文件<a>
  12.   <!–
  13.   <a href=”download.action?lname=DWHJ_062001.jpg”>下载文件</a>
  14.    –>
  15.   </s:iterator>
  16.   <br/><br/>
  17.   </body>
  18. </html>

 

 

action 下载代码:

 

[html][/html] view plaincopy

  1. package com.rui.struts;
  2. import java.io.InputStream;
  3. import java.io.UnsupportedEncodingException;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.apache.struts2.ServletActionContext;
  7. import com.opensymphony.xwork2.ActionSupport;
  8. public class DownLoald extends ActionSupport {
  9.     private String lname;
  10.     public String getLname() {
  11.         return lname;
  12.     }
  13.     public void setLname(String lname) {
  14.         this.lname = lname;
  15.     }
  16.     private String slname=”” ;
  17.     public InputStream getDownloadFile(){
  18.     HttpServletResponse response=ServletActionContext.getResponse();
  19.     try {
  20.         slname=new String(lname.getBytes(“ISO-8859-1″),”UTF-8”);
  21.     } catch (UnsupportedEncodingException e1) {
  22.         // TODO Auto-generated catch block
  23.         e1.printStackTrace();
  24.     }
  25.     try {
  26.         lname=java.net.URLEncoder.encode(slname,”utf-8″);
  27.     } catch (UnsupportedEncodingException e) {
  28.         // TODO Auto-generated catch block
  29.         e.printStackTrace();
  30.     }
  31.     System.out.println(“ddd”+lname);
  32.     response.setHeader(“Content-Disposition”,”attachment;filename=”+lname);
  33.         return ServletActionContext.getServletContext().
  34.         getResourceAsStream(“upload/”+slname);
  35.     }
  36.     //文件名如果有中文的话要进行uri中文转码
  37.             /*String encodFileNmae=””;
  38.             try {
  39.                  encodFileNmae=java.net.URLEncoder.encode(u.getOldname(),”utf-8″);
  40.             } catch (UnsupportedEncodingException e1) {
  41.                 // TODO Auto-generated catch block
  42.                 e1.printStackTrace();
  43.             }
  44.             //设置一个请求头告诉浏览器有文件要下载
  45.             response.setContentType(“text/html;charset=utf-8”);
  46.             response.setHeader(“Content-Disposition”,”attachment;filename=”+encodFileNmae);
  47.     */
  48.     @Override
  49.     public String execute() throws Exception {
  50.         return SUCCESS;
  51.     }
  52. }

 

标签