zoukankan      html  css  js  c++  java
  • 使用response.setHeader("Content-Disposition","attachment;filename="+fName)下载文件,中文文件名无法显示的问题

    今天遇到这么一个情况,在Action代码中进行文件下载:

      ActionForm得到file_id,通过file_id进行数据库查询得到file_name以及服务器硬盘上的file_uri,其中file_name是中文,然后通过如下代码下载

    response.setContentType("application/x-download");
    response.setHeader("Content-Disposition","attachment;filename="+file_name);
    OutputStream outputStream = response.getOutputStream();
    InputStream inputStream = new FileInputStream(file_uri());
    byte[] buffer = new byte[1024];
    int i = -1;
    while ((i = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, i);
    }
    outputStream.flush();
    outputStream.close();
    inputStream.close();

    假设file_name的内容是:“工程文档.docx”

    那么下载的结果就是一个名为“docx”的文件(文件名+后缀名),显然,文件名中的中文字符丢失了

    上网查了资料,解决办法如下(我也感觉很不可思议,但的确管用了)

    response.setContentType("application/x-download");
    file_name = new String(file_name.getBytes(), "ISO-8859-1");
    response.setHeader("Content-Disposition","attachment;filename="+file_name);
    // 其他代码略
  • 相关阅读:
    SQL中的数字格式化 (收藏)
    read about用法
    run into用法
    shoot for用法
    take off用法
    英语成语
    bring up用法
    satisfy with用法
    spend用法
    Linux环境进程间通信:共享内存
  • 原文地址:https://www.cnblogs.com/qrlozte/p/3553744.html
Copyright © 2011-2022 走看看