这篇文章将为大家详细讲解有关java下载文件乱码的解决方法,文章内容质量较高,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

String userAgent = request.getHeader("User-Agent");
String formFileName = file.getFileName();
// 针对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("multipart/form-data");
response.setCharacterEncoding("UTF-8");URLEncoder.encode(String s, String enc) :使用指定的编码机制将字符串转换为 application/x-www-form-urlencoded 格式 。
new String(byte[],decode)方法使用指定的编码decode来将byte[]解析成字符串。
到此为止, 关于java下载文件乱码的解决方法有了一个基础的认识, 但是对于具体的使用方法还是需要多加巩固和练习,如果想了解更多相关内容,请关注创新互联行业资讯。