Hendrik Söbbing
Hendrik Söbbing

Reputation: 142

java.io.FileNotFoundException when retrieving a url with umlauts in the filename

I am trying to retrieve a url with an umlaut in the filename, something like "http://somesimpledomain.com/some/path/überfile.txt", but it gives me a java.io.FileNotFoundException. I suspect that the filename on the remote server is encoded in latin1, though my url is in utf8. But my tries to change the encoding of the url weren't successful and I don't know how to debug it further. Please help!

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, Hendrik

Upvotes: 0

Views: 3785

Answers (2)

ZZ Coder
ZZ Coder

Reputation: 75496

URL needs to be properly encoded. You have to know what charset/encoding your server is expecting. You can try this first,

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

If that doesn't work, replace "ISO-8859-1" with "UTF-8" and try again.

If that doesn't work either, file doesn't exist :)

Upvotes: 4

daveb
daveb

Reputation: 76241

Have you tried urlencoding it? E.g.

%FCberfile

Upvotes: 0

Related Questions