glzmzd 2015-09-25 05:41 采纳率: 33.3%
浏览 1986
已采纳

下载文件乱码问题求助!!

 if(null!=page){
            try {
                getResponse().reset();// 清空输出流
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//"yyyy年MM月dd日HH时mm分ss秒"
            String formatFileName =URLEncoder.encode("Refrigerator cold chain alarm information"+ df.format(new Date()) +".xls","UTF-8");
            getResponse().setHeader("Content-disposition","attachment; filename="+formatFileName);// 设定输出文件头
                getResponse().setContentType("application/msexcel");// 定义输出类型
                OutputStream os = getResponse().getOutputStream();
                WritableWorkbook book = Workbook.createWorkbook(getResponse().getOutputStream());// 取得输出流
                @SuppressWarnings("unchecked")
                List<AlarmInfo>  mapList = (List<AlarmInfo>) page.getResults();
                this.editExcel(mapList,book);
                book.write(); // 写入文件
                book.close();
                os.close(); // 关闭流
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }

下载下来的文件空格会变成加号,时间里的冒号会变成 %3A。
我试过将FILENAME 转码后再用replace替换里面的字符的方法,但是结果就无法下载正确格式的文件了。
求助!
更新:使用replace将filename中的加号变成空格的方法,在谷歌上可行,在火狐上就会变成下载一个文件名Refrigerator,下载框中显示类型为 application/msexcel的无后缀文件了

  • 写回答

3条回答 默认 最新

  • Robot-C 2015-09-25 05:44
    关注

    方法1:
    response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
    方法2:
    response.se......
    答案就在这里:下载文件乱码问题
    ----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?