When Spring mvc does file download, it encounters a filename encoding problem. After Baidu, reference the following two articles, solve the coding problem.
http://www.iefans.net/xiazai-wenjian-http-bianma-content-disposition/
https://yq.aliyun.com/articles/38945
The final code is as follows:
PublicResponseentity<inputstreamresource>downloadFile (Path filePath)throwsfilenotfoundexception {File file=Filepath.tofile (); String MimeType=Urlconnection.guesscontenttypefromname (File.getname ()); if(MimeType = =NULL) {MimeType=Mediatype.application_octet_stream_value; } httpheaders respheaders=Newhttpheaders (); Respheaders.set (Httpheaders.content_type, MimeType); Respheaders.setcontentlength (File.length ()); String Encodedfilename=File.getname (); Try{encodedfilename= Urlencoder.encode (Encodedfilename, "UTF-8"); } Catch(unsupportedencodingexception e) {logger.error ("File name encoding Error!" ", E); } Respheaders.set (httpheaders.content_disposition, "attachment; Filename=\ "" + encodedfilename + "\" "+"; Filename*=utf-8 "" + encodedfilename); Inputstreamresource ISR=NewInputstreamresource (Newfileinputstream (file)); return NewResponseentity<inputstreamresource>(ISR, Respheaders, Httpstatus.ok); }
First press UTF-8 on the original file name, and then set content-disposition. Two filename is set in content-disposition to be compatible with more browsers.
File name encoding problem for Spring MVC file download