检索网址时java.io.FileNotFoundException在文件名变音
-
12-09-2019 - |
题
我想检索与文件名中的变音的url,像“的http://somesimpledomain.com/some/path/überfile.txt”,但它给了我一个java.io.FileNotFoundException。我怀疑,在远程服务器上的文件名中的latin1编码,虽然我的网址是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");
如果不工作,用 “UTF-8” 取代 “ISO-8859-1”,然后再试一次。
如果这也不起作用,文件不存在:)
其他提示
你试过URL编码呢?例如。
%FCberfile
不隶属于 StackOverflow