• 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"})})

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

  • 相关阅读:
    Java内存回收机制
    Java并发编程-synchronized指南
    Java Servlet完全教程
    Java线程池的那些事
    Java 代码性能优化总结
    Java开发必会的Linux命令
    Java多线程问题总结
    Redis 学习笔记续
    Redis 学习笔记
    Nginx配置文件详解
  • 原文地址:https://www.cnblogs.com/zhli/p/2816588.html
Copyright © 2020-2023  润新知