Java 文件下载解决中文文件名乱码的问题
private void download(HttpServletRequest request, HttpServletResponse response, String fileName){
    String fullPath = this.filePath + fileName;
    File downloadFile = new File(fullPath);
    if (!downloadFile.exists()){
        throw new CustomHttpException(10006);
    }

    ServletContext context = request.getServletContext();
    String mimeType = context.getMimeType(fullPath);
    if (mimeType == null) {
        mimeType = "application/octet-stream";
    }
    try {
        response.setContentType(mimeType);
        response.setContentLength((int) downloadFile.length());
        response.setCharacterEncoding("utf-8");
        String headerKey = "Content-Disposition";
        String headerValue = String.format(
                "attachment; filename=\"%s\"",
                URLEncoder.encode(downloadFile.getName(), "UTF-8")
        );
        response.setHeader(headerKey, headerValue);
        InputStream myStream = new FileInputStream(fullPath);
        IOUtils.copy(myStream, response.getOutputStream());
        response.flushBuffer();
    } catch (IOException ue){
        throw new CustomHttpException(10006);
    }
}
《Java 文件下载解决中文文件名乱码的问题》为 九城 原创,创作不易!转载请注明出处!感谢!
文章地址:https://blog.minkse.cn/java-%e6%96%87%e4%bb%b6%e4%b8%8b%e8%bd%bd%e8%a7%a3%e5%86%b3%e4%b8%ad%e6%96%87%e6%96%87%e4%bb%b6%e5%90%8d%e4%b9%b1%e7%a0%81%e7%9a%84%e9%97%ae%e9%a2%98/
暂无评论

发送评论 编辑评论

|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇