spring boot 下载文件时,文件乱码问题

老问题了,其实有很多解决方法,今在此记录一下,文件名乱码时的正解:(前提:项目采用的时UTF-8编码)

正解方法:

 String fileName = attachmentName;
 response.setContentType("multipart/form-data"); 
 //response.setContentType("multipart/form-data;charset=UTF-8");也可以明确的设置一下UTF-8,测试中不设置也可以。 
 response.setHeader("Content-Disposition", "attachment; fileName="+  fileName +";filename*=utf-8''"+URLEncoder.encode(fileName,"UTF-8"));

还有一种方式:

String fileName = attachmentName;
response.setContentType("multipart/form-data"); 
response.setHeader("Content-Disposition", "attachment;fileName="+ new String(fileName.getBytes("GB2312"),"ISO-8859-1"));  

第一中方式可以设置多个名称(比如支持IE6时,设置英文名称)


你可能感兴趣的:(springmvc)