Struts2上传下载

 

struts2 实现文件上传和下载

标签: struts2upload上传下载
 分类:
 

目录(?)[+]

 

项目目录:

web.xml:主要是配好struts2

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  5.     id="WebApp_ID" version="3.0">  
  6.     <display-name>uploadTest</display-name>  
  7.   
  8.     <!-- struts2配置 -->  
  9.     <filter>  
  10.         <filter-name>struts2</filter-name>  
  11.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  12.     </filter>  
  13.     <filter-mapping>  
  14.         <filter-name>struts2</filter-name>  
  15.         <url-pattern>/*</url-pattern>  
  16.     </filter-mapping>  
  17.   
  18.     <welcome-file-list>  
  19.         <welcome-file>index.jsp</welcome-file>  
  20.     </welcome-file-list>  
  21.   
  22. </web-app>  

 

struts.xml:配好上传下载的两个action。 

  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.   
  6. <struts>  
  7.   
  8.     <constant name="struts.devMode" value="true" />  
  9.   
  10.     <package name="default" namespace="/" extends="struts-default">  
  11.           
  12.         <!-- 上传 -->  
  13.         <action name="uploadAction" class="upload.UploadAction">  
  14.             <result>index.jsp</result>  
  15.             <result name="input">index.jsp</result>  
  16.   
  17.             <interceptor-ref name="fileUpload">  
  18.                 <!-- 不写下面两个参数为允许所有大小所有类型的文件的上传 -->  
  19.                 <param name="maximumSize">1024000</param>   
  20.                 <param name="allowedTypes">  
  21.                     application/msword,image/jpeg  
  22.                 </param>  
  23.             </interceptor-ref>  
  24.             <interceptor-ref name="defaultStack" />  
  25.   
  26.         </action>  
  27.           
  28.         <!-- 下载 -->  
  29.         <action name="downloadAction" class="upload.DownloadAction">  
  30.             <result type="stream">  
  31.                 <!-- 文件类型 —— application/octet-stream 表示无限制 -->  
  32.                 <param name="contentType">application/octet-stream</param>  
  33.                 <!-- 流对象名 —— 去找Action中的getInputStream方法 -->  
  34.                 <param name="inputName">inputStream</param>  
  35.                 <!-- 文件名 —— 将调用该Action中的getFileName方法 -->  
  36.                 <param name="contentDisposition">attachment;filename="${fileName}"</param>  
  37.                 <param name="bufferSize">4096</param>  
  38.             </result>  
  39.         </action>  
  40.     </package>  
  41.   
  42. </struts>  

 

UploadAction.java

 

 

[java] view plain copy
 
  1. package upload;  
  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.   
  9. import com.opensymphony.xwork2.ActionSupport;  
  10.   
  11. public class UploadAction extends ActionSupport {  
  12.     private static final long serialVersionUID = 1L;  
  13.   
  14.     private File myFile;//上传的文件,对应表单的file的name属性  
  15.     private String myFileContentType;//文件类型,xxxContentType,xxx对应表单file的name属性  
  16.     private String myFileFileName;  
  17.   
  18.     @Override  
  19.     public String execute() throws Exception {  
  20.         if (myFile == null) {  
  21.             this.addFieldError("file", "文件不能为空,请选择");  
  22.             return INPUT;  
  23.         } else {  
  24.             InputStream is = new FileInputStream(this.getMyFile());  
  25.             OutputStream os = new FileOutputStream(new File("F:/", this.getMyFileFileName()));  
  26.             byte[] buf = new byte[1024];  
  27.             int length = 0;  
  28.             while ((length = is.read(buf)) > 0) {  
  29.                 os.write(buf, 0, length);  
  30.             }  
  31.             is.close();  
  32.             os.close();  
  33.         }  
  34.         return SUCCESS;  
  35.     }  
  36.   
  37.     public File getMyFile() {  
  38.         return myFile;  
  39.     }  
  40.   
  41.     public void setMyFile(File myFile) {  
  42.         this.myFile = myFile;  
  43.     }  
  44.   
  45.     public String getMyFileContentType() {  
  46.         return myFileContentType;  
  47.     }  
  48.   
  49.     public void setMyFileContentType(String myFileContentType) {  
  50.         this.myFileContentType = myFileContentType;  
  51.     }  
  52.   
  53.     public String getMyFileFileName() {  
  54.         return myFileFileName;  
  55.     }  
  56.   
  57.     public void setMyFileFileName(String myFileFileName) {  
  58.         this.myFileFileName = myFileFileName;  
  59.     }  
  60.   
  61. }  

 

DownloadAction.java

 

 

[java] view plain copy
 
  1. package upload;  
  2.   
  3. import java.io.InputStream;  
  4.   
  5. import org.apache.struts2.ServletActionContext;  
  6.   
  7. import com.opensymphony.xwork2.ActionSupport;  
  8.   
  9. public class DownloadAction extends ActionSupport {  
  10.     private static final long serialVersionUID = 1L;  
  11.       
  12.     private String fileName;//要下载的文件名  
  13.     private InputStream inputStream;  
  14.   
  15.     @Override  
  16.     public String execute() throws Exception {  
  17.         inputStream = ServletActionContext.getServletContext().getResourceAsStream("/" + fileName);  
  18.         return SUCCESS;  
  19.     }  
  20.   
  21.     public String getFileName() {  
  22.         return fileName;  
  23.     }  
  24.   
  25.     public void setFileName(String fileName) {  
  26.         this.fileName = fileName;  
  27.     }  
  28.   
  29.     public InputStream getInputStream() {  
  30.         return inputStream;  
  31.     }  
  32.   
  33.     public void setInputStream(InputStream inputStream) {  
  34.         this.inputStream = inputStream;  
  35.     }  
  36. }  

 

index.jsp:访问:

 

 

[html] view plain copy
 
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib uri="/struts-tags" prefix="s" %>  
  4.   
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  6. <html>  
  7. <head>  
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  9. <title>Insert title here</title>  
  10. </head>  
  11. <body>  
  12.   
  13.     <!-- 上传 -->  
  14.     <s:form action="uploadAction" enctype="multipart/form-data" method="post">  
  15.         <s:file name="myFile"></s:file>  
  16.         <s:submit />  
  17.     </s:form>  
  18.       
  19.     <!-- 下载 -->  
  20.     <href="downloadAction?fileName=index.jsp">下载index.jsp文件</a>  
  21.       
  22. </body>  
  23. </html>  

 

 

参考博客:http://www.blogjava.net/thisliy/archive/2009/08/14/291153.html

posted @ 2017-06-08 13:57  此时的人生  阅读(173)  评论(0编辑  收藏  举报