About Java File download file name garbled problem solution

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.