qq_36406259
2021-07-10 01:12
采纳率: 100%
浏览 117
已采纳

javaweb 文件下载 附件名乱码 波浪线(~)显示为(_)

问题描述:chrome浏览器下载附件 附件名字中的字符(~)乱码
IE和火狐附件名正常,chrome版本:版本 91.0.4472.124(正式版本) (64 位)

@Controller
public class FileDown {

@RequestMapping("/down")
public void  downLoad(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String str = "C:\\Users\\34888\\Desktop\\新建文件夹\\1  -= 副本(3).txt";
    File file = new File (str);
    FileInputStream fileInputStream = new FileInputStream (file);
    ServletOutputStream outputStream = response.getOutputStream ();

    String encode = URLEncoder.encode ("锚123-~~~.txt", "UTF-8");
    response.setHeader ("Content-Disposition","attachement; filename="+ encode);

    int read = fileInputStream.read ();
    while(read>-1) {
        outputStream.write (read);
        read = fileInputStream.read ();
    }
}

}

实际下载:

img

img

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

查看更多回答(6条)

相关推荐 更多相似问题