springboot解决下载⽂件,⽂件名中⽂乱码问题
/**
* ⽂件下载
*
* @param id
* @return
*/
@GetMapping("/downloadFile")
@ApiOperation(value = "⽂件下载")
public void downloadFile(HttpServletRequest request,HttpServletResponse response,@RequestParam("userId")String userId,@RequestParam("fileId")String fileId) {
byte[] buffer = new byte[1024];
try {
FileBean bean = FileInfo(userId,fileId);
String filePath = FilePath();
File file = new File(filePath);
ists()) {
InputStream in = new FileInputStream(file);
setHeader(request,Name());
OutputStream outputStream = OutputStream();
ad(buffer,0,1024) != -1){
outputStream.write(buffer, 0, buffer.length);
}
}
fileStoreService.updateUserFile(userId,fileId,"download");
} catch (Exception e) {
<(e.getMessage());
}
}
private boolean setHeader(HttpServletRequest request, HttpServletResponse response, String fileName) {        try {
response.setContentType("application/octet-stream");
response.setHeader("content-type", "application/octet-stream");
String browser = Header("User-Agent");
if (-1 < browser.indexOf("MSIE 6.0") || -1 < browser.indexOf("MSIE 7.0")) {
// IE6, IE7 浏览器
response.addHeader("content-disposition", "attachment;filename="
+ new Bytes(), "ISO8859-1"));
} else if (-1 < browser.indexOf("MSIE 8.0")) {
// IE8
response.addHeader("content-disposition", "attachment;filename="
+ de(fileName, "UTF-8"));
} else if (-1 < browser.indexOf("MSIE 9.0")) {
// IE9springboot中文
response.addHeader("content-disposition", "attachment;filename="
+ de(fileName, "UTF-8"));
} else if (-1 < browser.indexOf("Chrome")) {
// ⾕歌
response.addHeader("content-disposition",
"attachment;filename*=UTF-8''" + de(fileName, "UTF-8"));
} else if (-1 < browser.indexOf("Safari")) {
// 苹果
response.addHeader("content-disposition", "attachment;filename="
+ new Bytes(), "ISO8859-1"));
} else {
// ⽕狐或者其他的浏览器
response.addHeader("content-disposition",
"attachment;filename*=UTF-8''" + de(fileName, "UTF-8"));
}
return true;
} catch (Exception e) {
<(e.getMessage());
return false;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。