文件下载及文件名为中文的时候各个浏览器出现乱码的解决方案


下面是jsp文件的代码:

   1. <html>
   2.   <head>
   3.    <title>download</title>
   4.   </head>
   5.   <body>
   6.    <%
   7.     //取得服务器"/download/file"目录的物理路径
   8.     String path = request.getRealPath("/download/file");
   9.     //取得"/download/file"目录的file对象
  10.     File file = new File(path);
  11.     //取得file目录下所有文件
  12.     File[] files = file.listFiles();
  13.  
  14.    for (int i = 0; i < files.length; i++) {
  15.  
  16.     String fname = files[i].getName();
  17.  
  18.     //对文件名进行url编码(UTF-8指明fname原来的编码,UTF-8一般由本地编码GBK代替)
  19.      fname = java.net.URLEncoder.encode(fname, "UTF-8");
  20.  
  21.     out.println("<a href=download.action?name=" + fname + ">"
  22.      + files[i].getName() + "</a><br>");
  23.     }
  24.    %>
  25.   </body>
  26.  </html>


 相应的DownloadAction.java的代码:

   1. package com.test.action;
   2.  
   3. import java.io.InputStream;
   4.  import java.io.UnsupportedEncodingException;
   5.  import com.opensymphony.xwork2.ActionSupport;
   6.  import org.apache.struts2.ServletActionContext;
   7.  
   8. public class DownloadAction extends ActionSupport {
   9.      private static final long serialVersionUID = 6329383258366253255L;
  10.  
  11.      private String fileName;
  12.  
  13.      public void setFileName(){
  14.          //得到请求下载的文件名
  15.          String fname=ServletActionContext.getRequest().getParameter("name");
  16.          try {
  17.          /*
  18.           * 对fname参数进行UTF-8解码,注意:实际进行UTF-8解码时会使用本地编码,本机为GBK。
  19.           * 这里使用request.setCharacterEncoding解码无效.
  20.           * 只有解码了getDownloadFile()方法才能在下载目录下正确找到请求的文件
  21.           * */  
  22.                fname = new String(fname.getBytes("ISO-8859-1"), "UTF-8");
  23.  
  24.        } catch (Exception e) {
  25.              e.printStackTrace();
  26.         }  
  27.         this.fileName=fname;  
  28.         System.out.println(fileName);
  29.     }
  30.  
  31.        /*
  32.         * @getFileName
  33.         * 此方法对应的是struts.xml文件中的:
  34.         * <param name="contentDisposition">attachment;filename="${fileName}"</param>
  35.         * 这个属性设置的是下载工具下载文件时显示的文件名,
  36.         * 要想正确的显示中文文件名,我们需要对fileName再次编码
  37.         * 否则中文名文件将出现乱码,或无法下载的情况
  38.         * */
  39.       public String getFileName() throws UnsupportedEncodingException {
  40.   
  41.           fileName=new String(fileName.getBytes(),"ISO-8859-1");
  42.   
  43.           return fileName;
  44.       }
  45.  
  46.      /*
  47.        * @getDownloadFile
  48.        * 此方法对应的是struts.xml文件中的:
  49.        * <param name="inputName">downloadFile</param>
  50.        * 返回下载文件的流,可以参看struts2的源码
  51.        * */
  52.       public InputStream getDownloadFile() {
  53.   
  54.          this.setFileName();
  55.  
  56.          return ServletActionContext.getServletContext().getResourceAsStream("/download/file/" + fileName);
  57.       }
  58.  
  59.      @Override
  60.       public String execute() throws Exception {
  61.           return SUCCESS;
  62.      }
  63.  }


struts.xml相应的Action配置:

   1. <action name="download" class="com.test.action.DownloadAction">
   2.       <result name="success" type="stream">
   3.           <param name="contentDisposition">attachment;filename="${fileName}"</param>
   4.           <param name="inputName">downloadFile</param>
   5.       </result>
   6.    </action>


web.xml:

   1. <filter>
   2.     <filter-name>struts2</filter-name>
   3.     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
   4.  </filter>
   5.   <filter-mapping>
   6.     <filter-name>struts2</filter-name>
   7.     <url-pattern>/*</url-pattern>
   8.   </filter-mapping>
   9.  
  10.   <welcome-file-list>
  11.     <welcome-file>download.jsp</welcome-file>
  12.   </welcome-file-list>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值