Springboot下载文件, 文件名中文是乱码, 空格变加号
默认把文件名放上去, 中文会乱码, 文件名种有空格, 就会被截断
public void download(HttpServletResponse response){
// 文件名先进行url编码, 避免乱码问题
// 把+用%20进行替换
fileName = URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
response.setContentType("application/octet-stream");
// 注意是 filename*=utf-8''
response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName + "." + fileRecord.getFileFormat());
}
不能直接把空格替换成%20, 因为%会被url编码转成%25, 虽然可以再手动替换回来, 但太麻烦了
就先url编码, 再替换+号即可
参考: https://blog.csdn.net/minshiwang/article/details/105393649