Java file download is garbled in two cases:
1, Chinese file name garbled when downloading
2, download because the path contains Chinese file name garbled, the prompt cannot find the file
The workaround is shown in the following section of code
Response.setcontenttype ("Multipart/form-data"); String useragent= Request.getheader ("User-agent"); String Orafilename=Meetingfile.getfilename (); String Formfilename=Orafilename; //For ie or IE as the kernel browser: if(Useragent.contains ("MSIE") | | useragent.contains ("Trident")) ) {Formfilename= Java.net.URLEncoder.encode (Formfilename, "UTF-8"); } Else { //non-IE browser processing:Formfilename =NewString (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; //To obtain a file object by pathFile File =NULL; if(Meetingfile! =NULL) {file=NewFile (path + "upload/" +orafilename); }
(1) If the first type of garbled characters, the download page encountered the following Chinese garbled problem
Use the following code to resolve
(2) If the download encounters a second garbled problem,
Fix it with the following code: first make sure that Tomcat, eclipse, and so on are UTF-8 encoded
Then, in Java, this is separated from the first one to encode the file names separately, so that they do not affect each other.
Note: Before the current test can solve the Firefox and IE and other browser download encoding problem, and for WIN10 comes from the browser will still appear garbled, who if there is a good way to be compatible with all browsers to share with me, grateful.
About Java File download file name garbled problem solution