zoukankan      html  css  js  c++  java
  • struts2 文件下载中文乱码问题已解决

    在使用struts2实现下载带有中文文字的文件时出现乱码问题,即弹出的对话框中文件名为乱码,而且无法无法下载,现就该问题给出如下解决方案:

    1、页面请求:<a id="download" href="follow!download?fileName=<s:property value="imageList0.imageName"/>"><s:property value="imageList0.imageName"/></a>

    2、action处理:

    private String fileName;

    public void setFileName(String fileName) {
    this.fileName = fileName;
    }

    public String getFileName() {
    try {
    fileName = new String(fileName.getBytes("GBK"), "ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    }
    return fileName;
    }

    /**
    * 文件下载
    */
    public String download(){
    return "download";
    }
    /**
    * 获取下载文件流
    * @return
    */
    public InputStream getInputStream(){
    String realPath = ServletActionContext.getServletContext().getRealPath("/upload") + "/" + fileName;
    InputStream inputStream = null;
    try {
    inputStream = new FileInputStream(new File(realPath));
    } catch (FileNotFoundException e) {
    log.error(e);
    }
    return inputStream;
    }

    返回处理:

    @Result(name = "download", type="stream", params={"contentType","application/octet-stream","inputName","inputStream","contentDisposition","attachment;filename=${fileName}","bufferSize","4096"})})

    重点是红色标注部分,否则一样不会解决乱码问题。

  • 相关阅读:
    不务正业系列-浅谈《过气堡垒》,一个RTS玩家的视角
    [LeetCode] 54. Spiral Matrix
    [LeetCode] 40. Combination Sum II
    138. Copy List with Random Pointer
    310. Minimum Height Trees
    4. Median of Two Sorted Arrays
    153. Find Minimum in Rotated Sorted Array
    33. Search in Rotated Sorted Array
    35. Search Insert Position
    278. First Bad Version
  • 原文地址:https://www.cnblogs.com/zhli/p/2816588.html
Copyright © 2011-2022 走看看