Java下载文件中文乱码的编码方式

标签:

本文出自jvm123.com-java技术分享站:http://jvm123.com/2020/05/java-xia-zai-wen.html

Java下载文件中文乱码的编码方式,使用URLEncoder进行编码后,中文就不会 乱码了。

    setDownloadName(response, "各行业在运行统计汇总表.xlsx");
    // URLEncoder编码
    protected void setDownloadName(HttpServletResponse response, String filename) {
        if (filename == null) {
            filename = "未命名";
        }
        String downFilename;
        try {
            downFilename = URLEncoder.encode(filename, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            downFilename = filename;
            e.printStackTrace();
        }
        response.addHeader("Content-Disposition", "attachment;filename=" + downFilename);

    }

发表评论