struts2多文件动态下载及中文解决方案

    技术2022-05-20  47

    来源:http://jianglubin1.blog.163.com/blog/static/13177713420101245426740/

     

    关于文件上传的例子,网上已经有很多,所以本文就不谈文件上传,重点是文件下载及文件名为中文的时候各个浏览器出现乱码的解决方案。

     

    下面是jsp文件的代码:

     

    相应的DownloadAction.java的代码: <html>  <head>   <title>download</title>  </head>  <body>   <%    //取得服务器"/download/file"目录的物理路径    String path = request.getRealPath("/download/file");    //取得"/download/file"目录的file对象    File file = new File(path);    //取得file目录下所有文件    File[] files = file.listFiles();   for (int i = 0; i < files.length; i++) {    String fname = files[i].getName();    //对文件名进行url编码(UTF-8指明fname原来的编码,UTF-8一般由本地编码GBK代替)     fname = java.net.URLEncoder.encode(fname, "UTF-8");    out.println("<a href=download.action?name=" + fname + ">"     + files[i].getName() + "</a><br>");    }   %>  </body> </html>  相应的DownloadAction.java的代码:

     

    package com.test.action;import java.io.InputStream; import java.io.UnsupportedEncodingException; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext;public class DownloadAction extends ActionSupport {     private static final long serialVersionUID = 6329383258366253255L;      private String fileName;      public void setFileName(){         //得到请求下载的文件名         String fname=ServletActionContext.getRequest().getParameter("name");          try {         /*          * 对fname参数进行UTF-8解码,注意:实际进行UTF-8解码时会使用本地编码,本机为GBK。          * 这里使用request.setCharacterEncoding解码无效.          * 只有解码了getDownloadFile()方法才能在下载目录下正确找到请求的文件          * */                 fname = new String(fname.getBytes("ISO-8859-1"), "UTF-8");       } catch (Exception e) {             e.printStackTrace();        }          this.fileName=fname;          System.out.println(fileName);     }        /*        * @getFileName        * 此方法对应的是struts.xml文件中的:        * <param >attachment;filename="${fileName}"</param>        * 这个属性设置的是下载工具下载文件时显示的文件名,        * 要想正确的显示中文文件名,我们需要对fileName再次编码        * 否则中文名文件将出现乱码,或无法下载的情况        * */      public String getFileName() throws UnsupportedEncodingException {            fileName=new String(fileName.getBytes(),"ISO-8859-1");            return fileName;      }     /*       * @getDownloadFile       * 此方法对应的是struts.xml文件中的:       * <param >downloadFile</param>       * 返回下载文件的流,可以参看struts2的源码       * */      public InputStream getDownloadFile() {           this.setFileName();         return ServletActionContext.getServletContext().getResourceAsStream("/download/file/" + fileName);      }     @Override      public String execute() throws Exception {          return SUCCESS;     } }

    struts.xml相应的Action配置:

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

    web.xml:

    <filter>    <filter-name>struts2</filter-name>    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter>  <filter-mapping>    <filter-name>struts2</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>   <welcome-file-list>    <welcome-file>download.jsp</welcome-file>  </welcome-file-list>


    最新回复(0)