I am writing a simple file download servlet and I can not find the correct file name. The tried URLNocoding and MimeEncoding filename have been seen in the current reply, but none of them work.
The following snippet contains mime type, byte [] content and file name in the file data object, which is the least required ISO-8859-2 charset, ISO-8859-1 is not enough.
How do I get my browser to correctly display the downloaded file name?
Here is an example of the file name: árvÃztÅ ± ra ?? Tükörfúrógà © p.xls and the result in it: árvÃztqrptükörfúrógà © p.xls Protected Zero RenderMergedOutputModel (Map Model, HttpServletRequest Request, HttpServletResponse Res) Exception {Rate Document File Data = (Rate document) throws model.get ("command.retval"); OutputStream Out = res.getOutputStream (); If (fileData! = Null) {res.setContentType (fileData.getMime ()); String enc = "UTF-8"; // Also tried: ISO-8859-2 string encoded filename = fileData. Gadgetname (); // also tried to encode this file name without URLencoding and successfully res.setCharacterEncoding (ANP); // Try this with res.setHeader and without ("content-dispute", "attachment; file name =" + encoded filename); Res.setContentLength (fileData.getBody () Length.); Out.write (fileData.getBody ()); } And {res.setContentType ("text / html"); Error downloading file & lt; / Body & gt; & Lt; / Html & gt; ".getBytes (res.getCharacterEncoding ());}} Out.flush ();}
I came to know that works in all the browsers I have installed (IE8, FF16, Opera 12, Chrome 22).
This is based on the fact that browsers have the value in the file name parameter Hopefully, which is encoded in native encoding in the browser, if any [different] encoding is not specified.
Usually the browser's native encoding UTF-8 (f) Firefox, Opera, Chrome). But the original encoding of IE is Win-1250.
So if we put values in file name parameters, which according to the user's browser is UTF-8 / win - 1250 is encoded, then it should work. At least, it works for me.
string filename = "¡¡¡¡¡¡¡¡RA ± RSA ?? Tac ¶ rr º º ³ ³ ³ ³ ³ ³ ³ ³ ³ "; string useragent = request.getHeader (" user-agent "); boolean is Internet Explorer = (userAgent.indexOf (" MSIE ")> -1 ); Try {byte [] fileNameBytes = fileName.getBytes ((InternetInteretExplorer)? ("Windows-1250"): ("utf-8"); string desposim filename = ""; (Byte B: filename bits) dispositionFileName + = (Char) (b & 0xff); string nature = "attachment; File name = \ "" + dispositionFileName + "\" "Response.setHeader (" content-type ", nature);} hold (unsupported encoding exception NCE) {// ... exception handling ...} Of course, it is only tested on the browser mentioned above and I can not guarantee it at 100% that it will work anytime in the browser.
Note # 1 (@ Fallon) : This URL is not valid for using the .exode () method, despite the name of the method, this URL- In the coding does not encode the string, but it forms the encoding into encode (form-encoding is similar to url-encoding and in many cases it produces the same result.) There are some differences, however, for example space You should use the URI class for the correct url-encoded string: URI Yuri = New ur E (empty, empty, "¡¡¡± ra ± r ± rsa ?? taaiaaaaaaaaaaaaaaaaaaaaaaaa", tap); println (uri.toASIi; >
Comments
Post a Comment