views:

328

answers:

2

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

A: 

Have you tried urlencoding it? E.g.

%FCberfile
daveb
+1  A: 

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 :)

ZZ Coder
I hoped to get around this, but you are right: It worked out great! Thank you very much!
Hendrik