Java.io.FileNotFoundException when getting url using umlauts in file name

I am trying to get the url with the umlaut in the file name, something like http://somesimpledomain.com/some/path/überfile. txt "but it gives me java.io.FileNotFoundException. I suspect the file name on the remote server is encoded in latin1, although my url is in utf8. But my attempts to change the url encoding were not successful, and I don't know how to debug them further. Please help!

The code is as follows:

HttpURLConnection conn = null; try { conn = (HttpURLConnection) new URL(uri).openConnection(); conn.setRequestMethod("GET"); } catch (MalformedURLException ex) {} } catch (IOException ex){} // Filter headers int i=1; String hKey; while ((hKey = conn.getHeaderFieldKey(i)) != null) { conn.getHeaderField(i); i++; } // Open the file and output streams InputStream in = null; OutputStream out = null; try { in = conn.getInputStream(); } catch (IOException ex) { ex.printStackTrace(); } try { out = response.getOutputStream(); } catch (IOException ex) { } 

Regards, Hendrick

0
java url encoding filenotfoundexception
source share
2 answers

The URL must be encoded correctly. You need to know what encoding / encoding your server expects. You can try it first

  String uri = "http://somesimpledomain.com/some/path/" + URLEncoder.encode(filename, "ISO-8859-1"); 

If this does not work, replace "ISO-8859-1" with "UTF-8" and try again.

If this does not work, the file does not exist :)

+3
source share

Have you tried using it? For example.

 %FCberfile 
0
source share

All Articles