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/