Struts2 文件上传下载 含下载时中文乱码

------------->>转载于;http://blog.csdn.net/zjw10wei321/article/details/9031417
对于struts2一直都是看过,很少去动手写里面的功能,今天花了一点时间算整理整理。

整体的项目大致如下:


1:文件上传

uploadAction.java


[java] view plaincopy
  1. package action;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7.   
  8. import org.apache.commons.io.FileUtils;  
  9. import org.apache.struts2.ServletActionContext;  
  10.   
  11. import com.opensymphony.xwork2.ActionSupport;  
  12.   
  13. public class UploadAction extends ActionSupport {  
  14.   
  15.     private static final long serialVersionUID = -8920466592471253212L;  
  16.   
  17.     private String title;  
  18.     private File file; // 上传的文件  
  19.     private String fileFileName; // 文件名称  
  20.     private String fileContentType; // 文件类型  
  21.     private String savePath;  
  22.     private String contents = "";  
  23.   
  24.     public String execute() throws Exception {  
  25.         // String url = getSavePath() + File.separator + getFileFileName();  
  26.         String url = getSavePath();  
  27.         System.out.println("url==" + url + ",,," + getFileContentType());  
  28.         FileOutputStream fos = null;  
  29.         FileInputStream fis = null;  
  30.         try {  
  31.             // fos = new FileOutputStream(url);  
  32.             File uploadFile = new File(url, this.getFileFileName());  
  33.             fos = new FileOutputStream(uploadFile);  
  34.   
  35.             fis = new FileInputStream(getFile());  
  36.             byte[] buffer = new byte[1024 * 1024];  
  37.             int len = 0;  
  38.             while ((len = fis.read(buffer)) > 0) {  
  39.                 fos.write(buffer, 0, len);  
  40.                 setContents(getContents() + new String(buffer));  
  41.             }  
  42.             fos.flush();  
  43.         } catch (Exception e) {  
  44.             e.printStackTrace();  
  45.             return INPUT;  
  46.         } finally {  
  47.             close(fos, fis);  
  48.         }  
  49.         return SUCCESS;  
  50.     }  
  51.   
  52.     public File getFile() {  
  53.         return file;  
  54.     }  
  55.   
  56.     public void setFile(File file) {  
  57.         this.file = file;  
  58.     }  
  59.   
  60.     public String getFileFileName() {  
  61.         return fileFileName;  
  62.     }  
  63.   
  64.     public void setFileFileName(String fileFileName) {  
  65.         this.fileFileName = fileFileName;  
  66.     }  
  67.   
  68.     public String getFileContentType() {  
  69.         return fileContentType;  
  70.     }  
  71.   
  72.     public void setFileContentType(String fileContentType) {  
  73.         this.fileContentType = fileContentType;  
  74.     }  
  75.   
  76.     public String getTitle() {  
  77.         return title;  
  78.     }  
  79.   
  80.     public void setTitle(String title) {  
  81.         this.title = title;  
  82.     }  
  83.   
  84.     public String getSavePath() {  
  85.         String result = ServletActionContext.getServletContext().getRealPath(  
  86.                 savePath);  
  87.         return result;  
  88.     }  
  89.   
  90.     public void setSavePath(String savePath) {  
  91.         this.savePath = savePath;  
  92.     }  
  93.   
  94.     public String getContents() {  
  95.         return contents;  
  96.     }  
  97.   
  98.     public void setContents(String contents) {  
  99.         this.contents = contents;  
  100.     }  
  101.   
  102.     private void close(FileOutputStream fos, FileInputStream fis) {  
  103.         if (fis != null) {  
  104.             try {  
  105.                 fis.close();  
  106.             } catch (IOException e) {  
  107.                 System.out.println("FileInputStream关闭失败");  
  108.                 e.printStackTrace();  
  109.             }  
  110.         }  
  111.         if (fos != null) {  
  112.             try {  
  113.                 fos.close();  
  114.             } catch (IOException e) {  
  115.                 System.out.println("FileOutputStream关闭失败");  
  116.                 e.printStackTrace();  
  117.             }  
  118.         }  
  119.     }  
  120. }  



上传页面upload.jsp:

[html] view plaincopy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@taglib prefix="s" uri="/struts-tags" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. >  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.      <title>上传文件title>  
  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.   head>  
  20.     
  21.   <body>  
  22.     <s:form action="uploads" method="post" enctype="multipart/form-data">  
  23.     <s:textfield id="title" name="title" label="文件标题"/>  
  24.     <s:file name="file" label="上传文件"/>  
  25.     <s:submit value="提交"/>  
  26.     s:form>  
  27.   
  28.   body>  
  29. html>  

上传成功uploadSuccess.jsp:


[html] view plaincopy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@taglib prefix="s" uri="/struts-tags" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. >  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>My JSP 'index.jsp' starting pagetitle>  
  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.   head>  
  20.     
  21.   <body>  
  22.     上传文件:<s:property value="title" /><br/>  
  23.     文件内容:  
  24.     <s:if test="%{fileContentType.indexOf('image')>=0}">  
  25.         <img src="<s:property value="'upload/'+fileFileName"/>"/>  
  26.     s:if>  
  27.     <s:else>  
  28.         <s:property value="contents"/>  
  29.     s:else>  
  30.   body>  
  31. html>  

主要的struts.xml:



[html] view plaincopy
  1. xml version="1.0" encoding="UTF-8" ?>  
  2. >  
  3. <struts>  
  4.     <constant name="struts.devMode" value="true" />  
  5.     <constant name="struts.i18n.encoding" value="UTF-8" />  
  6.       
  7.     <package name="myDemo" extends="struts-default" namespace="/">  
  8.       
  9.         <action name="uploads" class="action.UploadAction" >  
  10.             <param name="savePath">/uploadparam>  
  11.             <result name="input">/upload.jspresult>  
  12.             <result name="success">/uploadSuccess.jspresult>  
  13.         action>  
  14.         <action name="downloads" class="action.DownloadAction">  
  15.             <param name="inputPath">uploadparam>  
  16.             <result name="input">/downloadSuccess.jspresult>  
  17.             <result name="success" type="stream">  
  18.                 <param name="contentType">application/octet-stream;charset=ISO8859-1 param>  
  19.                 <param name="inputName">downloadFileparam>  
  20.                 <param name="contentDisposition">attachment;filename="${downloadFileName}"param>  
  21.                 <param name="bufferSize">4096param>  
  22.             result>  
  23.         action>  
  24.           
  25.         <action name="download" class="action.DownloadAction" method="download">  
  26.             <param name="inputPath">uploadparam>  
  27.             <result name="success" >/download.jsp  
  28.             result>  
  29.         action>  
  30.   
  31.