java.io.FileNotFoundException在文件名中检索带有变音符号的URL时

我试图在文件名中检索带有变音符号的URL,例如“ http://somesimpledomain.com/some/path/überfile.txt ”,但它给了我一个java.io.FileNotFoundException。 我怀疑远程服务器上的文件名是用latin1编码的,尽管我的url是在utf8中。 但我尝试更改url的编码并不成功,我不知道如何进一步调试它。 请帮忙!

代码如下:

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) { } 

此致,亨德里克

URL需要正确编码。 您必须知道服务器期望的字符集/编码。 你可以先试试这个,

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

如果不起作用,请将“ISO-8859-1”替换为“UTF-8”,然后重试。

如果这也不起作用,文件不存在:)

你试过urlencoding吗? 例如

 %FCberfile