在文件名中检索到带有变音符号的URL时,将返回java.io.FileNotFoundException [英] java.io.FileNotFoundException when retrieving a url with umlauts in the filename
问题描述
代码如下:
HttpURLConnection conn =空值;
try {
conn =(HttpURLConnection)new URL(uri).openConnection();
conn.setRequestMethod(GET);
} catch(MalformedURLException ex){}
} catch(IOException ex){}
//过滤头
int i = 1;
String hKey;
while((hKey = conn.getHeaderFieldKey(i))!= null){
conn.getHeaderField(i);
i ++;
}
//打开文件并输出流
InputStream in = null;
OutputStream out = null;
尝试{
in = conn.getInputStream();
} catch(IOException ex){
ex.printStackTrace();
}
try {
out = response.getOutputStream();
} catch(IOException ex){
}
问候,
Hendrik
URL需要正确编码。你必须知道您的服务器期望的字符集/编码。你可以先尝试一下,
String uri =http://somesimpledomain.com/some/path/+
URLEncoder.encode(filename,ISO-8859-1);
如果不行,请将ISO-8859-1替换为UTF-8然后再试一次。
如果还不行,文件不存在:)
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
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 :)
这篇关于在文件名中检索到带有变音符号的URL时,将返回java.io.FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!