JAVA文件下载时中文文件名乱码有两种情况:
下载时中文文件名乱码;
下载时因为路径中包含中文文件名乱码,提示找不到文件。
相关视频教程分享:java视频教程
解决方法如下:
response.setContentType("multipart/form-data");
String userAgent = request.getHeader("User-Agent");
String oraFileName = meetingFile.getFileName();
String formFileName=oraFileName;
// 针对IE或者以IE为内核的浏览器:
if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
formFileName = java.net.URLEncoder.encode(formFileName, "UTF-8");
} else {
// 非IE浏览器的处理:
formFileName = new String(formFileName.getBytes("UTF-8"), "ISO-8859-1");
}
response.setHeader("Content-disposition",
String.format("attachment; filename="%s"", formFileName));
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("UTF-8");
ServletOutputStream out;
// 通过文件路径获得File对象
File file = null;
if (meetingFile != null) {
file = new File(path + "upload/"+oraFileName);
}
相关文章教程分享:java入门教程