gpt4 book ai didi

java - FileNotFound 除非获取带有重音 Java 的 URL 资源

转载 作者:行者123 更新时间:2023-11-29 22:30:57 27 4
gpt4 key购买 nike

当我尝试下载文件 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)

我是否必须避免口音或其他什么?

最佳答案

解决方案(以我为例):

如果服务器响应代码 >= HTTP_BAD_REQUEST(大于 400),类 HttpURLConnectionImpl 的方法 getInputStream() 将抛出FileNotFoundException(因此您无法打开输入流)。

即使此文件存在,您的对象也不会为您提供输入流,因为服务器响应代码 >=400 - 更改服务器上的响应代码使用其他类 连接。

源代码 fragment : http://www.docjar.com/html/api/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnectionImpl.java.html

  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 }

关于java - FileNotFound 除非获取带有重音 Java 的 URL 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4075152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com