当我尝试下载文件http://tfob.azstarnet.com/images/authors/Alcal%C3%A1_Kathleen_small.jpg时,我收到了FileNotFoundException。问题几乎可以肯定,URL在字符串中有重音字符。我该如何处理?
我正在下载它。
Log.d(TFOB.TAG, "Image src: " + desc.getString("image"));
productURL = new URL (desc.getString("image").trim());
prod = productURL.openConnection();
is = prod.getInputStream(); // Exception gets thrown here
bis = new BufferedInputStream(is);
bit = BitmapFactory.decodeStream(bis);
这是堆栈跟踪:
Image src: http://tfob.azstarnet.com/images/authors/Alcalá_Kathleen_small.jpg
java.io.FileNotFoundException: http://tfob.azstarnet.com/images/authors/Alcalá_Kathleen_small.jpg
org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1162)
我是否必须摆脱口音或什么?
答案 0 :(得分:2)
解决方案(在我的情况下):
如果服务器响应代码是> = HTTP_BAD_REQUEST(大于400),则类HttpURLConnectionImpl的方法getInputStream()将抛出 FileNotFoundException(因此无法打开输入流)。
即使此文件存在,您的对象也不会为您提供输入流,因为服务器响应代码为> = 400 - 更改服务器上的响应代码或使用其他类强>连接。
867 @Override
868 public InputStream getInputStream() throws IOException {
869 if (!doInput) {
870 throw new ProtocolException(Messages.getString("luni.28")); //$NON-NLS-1$
871 }
872
873 // connect before sending requests
874 connect();
875 doRequest();
876
...
883 if (responseCode >= HTTP_BAD_REQUEST) {
884 throw new FileNotFoundException(url.toString());
885 }
886
887 return uis;
888 }
答案 1 :(得分:0)
答案 2 :(得分:0)
我用残酷的方法解决了问题:
private Drawable LoadImageFromWebOperations(String strPhotoUrl) {
try{
String lnk = strPhotoUrl;
lnk = lnk.replaceAll("à","%C3%A0");
lnk = lnk.replaceAll("è","%C3%A8");
lnk = lnk.replaceAll("è","%C3%A9");
lnk = lnk.replaceAll("ì","%C3%AC");
lnk = lnk.replaceAll("ò","%C3%B2");
lnk = lnk.replaceAll("ù","%C3%B9");
Log.i("Tommy", lnk+"\n");
InputStream is = (InputStream) new URL(lnk).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
//System.out.println("Exc=" 2e);
Log.i("Tommy", strPhotoUrl+"\n");
Log.i("Tommy", e.toString() + "\n\n");
return null;
}
}