0

I ran into a problem when I call request.getParameter( "filename" ) in a HttpServletRequest in GWT. Here's the code how i encode the URL:

String sFile = URL.encodeQueryString( "°^!§$%()=`´' glassfish +~#'@€-_²³.pdf" );
String sURL = GWT.getModuleBaseURL() + "filehttpservice" // name of the httpservlet
    + "?filename=" + sFile; // the name of the file to download
Window.open( sURL, "_blank", sFeatures ); // sFeatures are some window-settings

So i want to download a file with some special characters in its name. The URL-encoded name is:

%C2%B0%5E!%C2%A7%24%25()%3D%60%C2%B4'%20glassfish%20%2B~%23'%40%E2%82%AC-_%C2%B2%C2%B3.pdf

which is correct, cause I can call the file directly in the browser with this name.

So when the request comes to the get-Method of the HttpServlet i want to extract the filename from its parameters with the following code:

request.setCharacterEncoding( "UTF-8" );
String sFilename = request.getParameter( "filename" );

But the received Filename is:

°^!§$%()=`´' glassfish +~#'@â¬-_²³.pdf

which is totally wrong.

I have searched for a long time and tried serveral solutions but it changes nothing. Does anybody have an idea how i can receive the correct filename?

N43
  • 3
  • 5

2 Answers2

0

request.setCharacterEncoding( "UTF-8" ); has no effect on doGet() . In doGet() the queryString gets parsed by the container before it reaches doGet() .

You should use doPost and request.getInputStream() and parse the queryString yourself. And do not use request.getParameter() before request.getInputStream() or it won't work.

Edit Java by default encodes String in utf-16 .. So you will have to convert it to utf-8 .

response.setHeader( "Content-Disposition", new String("attachment; filename=\"" + sUrlFilename + ".pdf" + "\"".getBytes("utf-8"),"ISO-8859-1") );

Anurag Anand
  • 500
  • 1
  • 7
  • 13
  • Ok but if I use `doPost` the decoding fails anyway. I get exactly the same wrong filename. It seems that it has nothing to do with the CharacterEncoding – N43 Nov 21 '14 at 16:02
  • @N43 ` URLDecoder .decode(new String(request.getParameter("filename").getBytes( "iso-8859-1")), CHARSET_FOR_URL_ENCODING);` you can read more here http://stackoverflow.com/questions/469874/how-do-i-correctly-decode-unicode-parameters-passed-to-a-servlet – Anurag Anand Nov 21 '14 at 18:36
  • Thanks! Now I decode the parameters myself correctly but face another problem with the response: When I set the response-Header `response.setHeader( "Content-Disposition", "attachment; filename=\"" + sUrlFilename + ".pdf" + "\"" );` where the sUrlFilename is the correct UTF-8-Filename: `°^!§$%()=´' glassfish +~#'@€-_²³` and the ContentType is `application/pdf; charset=UTF-8` the Filename in the download-prompt shows `^! $%()= ' glassfish +~#'@ -_ .pdf` which is also incorrect. Do you have any idea how to fix that? (Oh and `response.setCharacterEncoding("UTF-8")` was also called) – N43 Nov 24 '14 at 11:46
  • @N43 You are concatenating a `utf-8` and `utf-16` strings .. Java by default encodes `String` in `utf-16` .. So you need to convert whole String to `utf-8` . I have updated my answer. – Anurag Anand Nov 24 '14 at 13:22
  • For some reasons the byte-encoding in UTF-8 doesn't work for me. I resolved it using `MimeUtility.encodeWord( sUrlFilename, "ISO-8859-1", "Q" );`. However, thank you very much! Made my day :) – N43 Nov 24 '14 at 14:32
0

As Anurag Anand said, this is an encoding issue; you have to configure your servlet container to decode URLs as UTF-8.

With Tomcat for example, this is configured at the Connector level with the URIEncoding attribute. With Jetty, this can be set with the org.eclipse.jetty.util.UrlEncoding.charset system property.

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164