Localized Java File Names

How can I set localized file names in java.Currently every time I click on a localized file called non-ascii in my application, the Windows save dialog opens, but it does not display the file name correctly if the encoding is something above ISO-88859-1.

This is my code that saves the file.

InputStream inputStream = null; try { response.resetBuffer(); response.setContentType(fileStream.getContentType()); response.setContentLength((int) fileStream.getContentLength()); response.addHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\""); ServletOutputStream stream = response.getOutputStream(); byte[] buffer = new byte[1024]; int read = 0; int total = 0; inputStream = fileStream.getInputStream(); while ((read = inputStream.read(buffer)) > 0) { stream.write(buffer, 0, read); total += read; } response.flushBuffer(); } finally { if (inputStream != null) { inputStream.close(); } } 

I would be very helpful if someone could share their ideas on how to solve this problem. Thanks in advance.

+6
java servlets utf-8 character-encoding download
source share
4 answers

What gustafc says is correct, but it does not give you where you want to be. RFC 2231 allows you to use an alternative format for parameters without ASCII Content-Type and Content-Disposition , but not all browsers support it. Unfortunately, the way that is most likely to work is to ignore the fact that RFC 2183 speaks and uses RFC 2047 encoded words in the answer:

 response.addHeader("Content-Disposition", "attachment; " + "filename=\"" + MimeUtility.encodeWord(fileName, "utf-8", "Q") + "\""); 

Please note that this may not work for all browsers. Some variants of IE require you to convert the URL to a value:

 response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "utf-8")); 
+13
source share

I ran into similar problems with file names containing Greek characters. I used the code provided in the answer above (thanks to dkarp) in combination with detecting which browser is being used. this is the result:

  String user_agent = request.getHeader ("user-agent");
 boolean isInternetExplorer = (user_agent.indexOf ("MSIE")> -1);
 if (isInternetExplorer) {
     response.setHeader ("Content-disposition", "attachment; filename = \" "+ URLEncoder.encode (filename," utf-8 ") +" \ "");
 } else {
     response.setHeader ("Content-disposition", "attachment; filename = \" "+ MimeUtility.encodeWord (filename) +" \ "");
 }

I tested it with firefox 3.6, chrome 10.0 and Internet Explorer 8, and it seems to work fine.

+4
source share

From section 2.3 in the specification, it seems that you cannot use characters other than US-ASCII:

The current [RFC 2045] grammar limits the parameter values ​​(and therefore Content-Disposition filenames) in US-ASCII. We recognize the desirability of allowing arbitrary character sets in file names, but go beyond the scope of this document to determine the necessary mechanisms. We expect that the underlying [RFC 1521] `value 'specification will someday be changed to allow the use of non-US-ASCII characters, and at this point the same mechanism should be used in the file-name parameter.

+2
source share

Although an old question, it is still relevant. I found a solution that works for all my browsers.

See my post in another topic:
Java servlet download special character file name

In short, browsers expect the value in the filename parameter to be encoded in the source encoding of the browsers (unless otherwise specified for the filename parameter). The native encoding of the browser is usually utf-8 (FF, Opera, Chrome), but for IE it is win-1250. Therefore, if we put the value in filename parametr, which is encoded by utf-8 / win-1250 according to the user's browser, it should work.

For example, if we have a file called omáčka.xml ,
for FireFox, Opera and Chrome I will answer this header (encoded in utf-8):

 Content-Disposition: attachment; filename="omáčka.xml" 

and for IE I answer this header (encoded in win-1250):

 Content-Disposition: attachment; filename="omáèka.jpg" 

Java example in my post mentioned above.

Note # 1 (@dkarp) :

Keep in mind the use of URLEncoder.encode (), as this method does not encode the input string into url encoding. This method encodes the input lines into an encoding form that is very similar, but different in some cases - for example, the space character '' is encoded as "+" instead of "% 20".

To perform the correct url encoding, you should use the URI class:

 URI uri = new URI(null, null, "foo-ä-€.html", null); System.out.println(uri.toASCIIString()); 
+1
source share

All Articles