struts2文件上传下载


  1. package com.wansha.struts.action;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8. import java.util.HashMap;  
  9. import java.util.List;  
  10. import java.util.Map;  
  11.   
  12. import org.apache.struts2.ServletActionContext;  
  13.   
  14. import com.opensymphony.xwork2.ActionSupport;  
  15.   
  16. public class UploadAction extends ActionSupport{  
  17.     private String username;  
  18.     private List<File> file;  
  19.     private List<String> fileFileName;  
  20.     private List<String> fileContentType;  
  21.   
  22.     public String getUsername() {  
  23.         return username;  
  24.     }  
  25.     public void setUsername(String username) {  
  26.         this.username = username;  
  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.       
  47.     @Override  
  48.     @SuppressWarnings("deprecation")  
  49.     public String execute() throws Exception {  
  50.           
  51.         for(int i=0; i<file.size(); i++){  
  52.             String path = ServletActionContext.getRequest().getRealPath("/upload");  
  53.             System.out.println("filepath-->"+file.get(i).getAbsolutePath());  
  54.             File directory = new File(path);  
  55.             if(!directory.exists())directory.mkdirs();  
  56.             OutputStream os = new FileOutputStream(new File(path,fileFileName.get(i)));  
  57.             InputStream is = new FileInputStream(file.get(i));  
  58.             byte[] by = new byte[4096];  
  59.             int length = 0;  
  60.             while(-1 != (length = is.read(by,0,by.length))){  
  61.                 os.write(by,0,length);  
  62.             }  
  63.             os.close();  
  64.             is.close();  
  65.               
  66.         }  
  67.         return SUCCESS;  
  68.     }  
  69. }  


文件上传页面



  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3.   
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  5. <html>  
  6.   <head>  
  7.     <title>My JSP 'index.jsp' starting page</title>  
  8.     <meta http-equiv="pragma" content="no-cache">  
  9.     <meta http-equiv="cache-control" content="no-cache">  
  10.     <meta http-equiv="expires" content="0">      
  11.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  12.     <meta http-equiv="description" content="This is my page">  
  13.   </head>  
  14.     
  15.   <body>  
  16.     <s:form action="fileUpload" enctype="multipart/form-data" theme="simple" method="post">  
  17.         <s:textfield name="username" /><br />  
  18.         <s:file name="file" /><br />  
  19.         <s:file name="file" /><br />  
  20.         <s:file name="file" /><br />  
  21.         <s:submit value="提交" />  
  22.     </s:form>  
  23.   </body>  
  24. </html>  

文件上传后的结果 页面:


  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3.   
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  5. <html>  
  6.   <head>  
  7.     <title>My JSP 'uploadresult.jsp' starting page</title>  
  8.       
  9.     <meta http-equiv="pragma" content="no-cache">  
  10.     <meta http-equiv="cache-control" content="no-cache">  
  11.     <meta http-equiv="expires" content="0">      
  12.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  13.     <meta http-equiv="description" content="This is my page">  
  14.     <!-- 
  15.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  16.     -->  
  17.   
  18.   </head>  
  19.     
  20.   <body>  
  21.     username:<s:property value="username" />  
  22.     <s:iterator value="fileFileName" id="f">  
  23.         <s:property value="#f" />  
  24.     </s:iterator>  
  25.   </body>  
  26. </html>  



DownloadFileAction.java   文件下载



  1. package com.wansha.struts.action;  
  2.   
  3. import java.io.File;  
  4. import java.io.InputStream;  
  5. import java.io.UnsupportedEncodingException;  
  6.   
  7. import org.apache.struts2.ServletActionContext;  
  8.   
  9. import com.opensymphony.xwork2.ActionSupport;  
  10.   
  11. public class DownloadFileAction extends ActionSupport {  
  12.     private String fileName;  
  13.     private InputStream is;  
  14.   
  15.     public String getFileName() throws UnsupportedEncodingException {  
  16.         return new String(this.fileName.getBytes("UTF-8"),"8859_1");  
  17.     }  
  18.   
  19.     public void setFileName(String fileName) throws UnsupportedEncodingException {  
  20.         this.fileName = new String(fileName.getBytes("8859_1"),"UTF-8");  
  21.     }  
  22.       
  23.     public InputStream getDownloadFile(){  
  24.         this.is = ServletActionContext.getServletContext().getResourceAsStream("/upload/"+this.fileName);  
  25.         return this.is;  
  26.     }  
  27.       
  28.     public boolean isFile(){  
  29.         File file = new File("/upload/",this.fileName);  
  30.         return file.exists();  
  31.     }  
  32.       
  33.     @Override  
  34.     public String execute() throws Exception {  
  35.         if(isFile()){  
  36.             return INPUT;  
  37.         }  
  38.         return SUCCESS;  
  39.     }  
  40. }  



struts.xml


  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.1.7.dtd">  
  5. <struts>  
  6.     <constant name="struts.multipart.saveDir" value="/upload"></constant><!-- 临时存储文件夹 -->  
  7.     <constant name="struts.multipart.maxSize" value="209715200"></constant><!-- 最大上传大小-->  
  8.     <package name="view" extends="struts-default">  
  9.         <global-results>  
  10.             <result name="filenotfound">/filenotfound.jsp</result>  
  11.         </global-results>  
  12.         <global-exception-mappings>  
  13.             <exception-mapping result="filenotfound" exception="com.wansha.struts.exception.UnKnowErrorException"></exception-mapping>  
  14.         </global-exception-mappings>  
  15.         <action name="fileUpload" class="com.wansha.struts.action.UploadAction">  
  16.             <result name="success">/uploadresult.jsp</result>  
  17.         </action>  
  18.         <action name="download" class="com.wansha.struts.action.DownloadFileAction">  
  19.             <result type="stream">  
  20.                 <param name="contentDisposition">attachment;filename=${fileName}</param>  
  21.                 <param name="inputName">downloadFile</param>  
  22.             </result>  
  23.             <result name="input">/filenotfound.jsp</result>  
  24.         </action>  
  25.     </package>  
  26. </struts>  



downloadFile.jsp


  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'downloadFile.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.     <a href="download?fileName=未命名.jpg">未命名.jpg文件下载</a><br />  
  27.     <a href="download?fileName=appstore2.sql">appstore2.sql文件下载</a><br />  
  28.     <a href="download?fileName=数据结构(Java).pdf">数据结构(Java).pdf文件下载</a><br />  
  29.     <a href="download?fileName=site.xml">site.xml文件下载</a><br />  
  30.   </body>  
  31. </html>  

filenotfound.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  3. <html>  
  4.   <head>  
  5.     <title>My JSP 'filenotfound.jsp' starting page</title>  
  6.     <meta http-equiv="pragma" content="no-cache">  
  7.     <meta http-equiv="cache-control" content="no-cache">  
  8.     <meta http-equiv="expires" content="0">      
  9.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  10.     <meta http-equiv="description" content="This is my page">  
  11.     <!-- 
  12.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  13.     -->  
  14.   </head>  
  15.     
  16.   <body>  
  17.   找不到该文件!  
  18.   </body>  
  19. </html>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值