Solve the major browser download files garbled and Firefox download file name is not the whole problem

Source: Internet
Author: User

Recent projects, using spring MVC to do the control layer, the file name of the download is always in the mainstream browser display garbled, Firfox is not garbled, IE has become garbled. is also annoying, ie is always unique, no way, can only let the program to adapt, in the Internet also search a lot, but the solution is not ideal, mainly IE11 as the mainstream browser, a lot of people before the proposal has been invalidated.
Simply put, before using request to get the header of the user-agent, through the MSIE keyword to determine whether it is IE browser, if it is IE browser, using Utf-8 to encode the file name can be. However, IE11 User-agent has no MSIE keyword, the following is the request header of IE11:

User-Agent:Mozilla/5.0 (WindowsNT6.3WOW64Trident/7.0LCJBrv:11.0Gecko

As you can see, the MSIE keyword is no longer available, but we can also use the Trident keyword to determine
However, I use WIN10 on the Edge browser to download, is still garbled, look carefully, the original Edge browser user-agent has changed,MSIE and Trident keywords are not, increased Edge keyword, currently I use the computer or WIN8, here does not post the latest Edge browser request header, interested in the developer mode to see.

The ultimate Solution

OK, now we need to use the Trident and the Edge keyword to determine whether it is Microsoft's browser (Microsoft abandoned IE, started using edge), the code is as follows:

public  class  httputils {private  static  string[] Iebrowsersignals = {,  "Trident" ,  "Edge" }; public  static  boolean ismsbrowser  (httpservletrequest Request) {String useragent = Request.getheader ( "user-agent" ); for  (String signal:iebrowsersignals) {if  (Useragent.contains (signal)) r Eturn  true ; } return  false ; }}

Then in the download, first of all to determine whether it is Microsoft's browser, if so, with Utf-8 to encode the file name, if not, use the universal Solution garbled code:

newString(fileName.getBytes("UTF-8""ISO-8859-1")

So let's summarize, the final download code is as follows:

 response.setContentType("application/octet-stream"); boolean isMSIE = HttpUtils.isMSBrowser(request);  if (isMSIE) {                "UTF-8");            else {                newString(fileName.getBytes("UTF-8""ISO-8859-1");            }            response.setHeader("Content-disposition""attachment;filename=\"""\"");            //剩下的就是将文件流输出到response

This allows you to download files in the current Ie8-ie11, Edge, Firefox, and Chrome browsers, and the Chinese characters are no longer garbled.

Firefox download English + chinese combination filename problem

In addition, there is a need to note that this sentence:

response.setHeader("Content-disposition","attachment;filename=\"" + fileName + "\"");

When setting the Content-disposition entry for response, the value of filename is prefixed with double quotation marks, if the file name is a combination of English + Chinese, such as:Dota2 's Beginner's guide, if you do not add double quotation marks to Firefox when downloading files. docx , when the file is downloaded in Firefox, the downloaded filename is only dota2 . Only the double quotes are added, and the file name matches the filename set by the code. Because this double quote is in the string, it needs to be escaped by adding a backslash \.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Solve the major browser download files garbled and Firefox download file name is not the whole problem

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.