尝试从网络下载图像时出现FileNotFoundException异常

3
这里是代码。
package downloader;

import java.net.MalformedURLException;
import java.net.URL;
import java.io.File;
import java.io.IOException;

public class JustATest {
    public static void main(String[] args) throws IOException {
        File file = new File("D:" + "//" + "Hunter x Hunter 340 - 00 - créditos.jpg");
        URL url = new URL("http://leitor1.mangasproject.com/3e1e967e9b793e908f8eae83c74dba9bcccce6a5535b4b462bd9994537bfe15c/1c96b0ef48b44ff71102d96f7ac2b515a0b7be31d04d7420f3d133d923189953/Hunter x Hunter 340 - 00 - créditos.jpg");
        org.apache.commons.io.FileUtils.copyURLToFile(url, file);
    }
}

运行这段代码时,我遇到了以下错误:

    Exception in thread "main" java.io.FileNotFoundException: http://leitor1.mangasproject.com/3e1e967e9b793e908f8eae83c74dba9bcccce6a5535b4b462bd9994537bfe15c/1c96b0ef48b44ff71102d96f7ac2b515a0b7be31d04d7420f3d133d923189953/Hunter x Hunter 340 - 00 - créditos.jpg
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at org.apache.commons.io.FileUtils.copyURLToFile(FileUtils.java:1460)
at downloader.JustATest.main(JustATest.java:14)

我尝试使用。
InputStream in = new BufferedInputStream(url.openStream()); 

但这也不起作用。

编辑: 我必须说现在在NetBeans中所有的代码都可以正常工作,但是,我正在使用Eclipse编码。 :( 我还必须说,在Eclipse中,这个主机的大多数文件都可以正常工作。


1
如果您在浏览器中输入路径,它会将其编码为http://leitor1.mangasproject.com/3e1e967e9b793e908f8eae83c74dba9bcccce6a5535b4b462bd9994537bfe15c/1c96b0ef48b44ff71102d96f7ac2b515a0b7be31d04d7420f3d133d923189953/Hunter%20x%20Hunter%20340%20-%2000%20-%20cr%C3%A9ditos.jpg,然后再发送请求。我不知道那是什么编码方式,但如果您将该路径放入Java代码中,它将能够正常工作。 - Sotirios Delimanolis
6个回答

2
尝试将URLEncoder.encode(IMAGE_URL, "UTF-8")传递给URL,而不是仅传递普通的图像URL。

好的,我会尝试一下。可能是因为文件名(é)的原因。 - Killer
嗯,我尝试过了,使用sout(syso)可以看到URL已编码。但是,当inputStream打开流时,错误再次发生。 - Killer

1
可能是因为该网站不允许文件盗链。要解决这个问题 - 如果您有使用该网站的权限,可以使用Apache的HttpComponents首先导航到主页或包含图像的页面。不需要下载所有js和css,只需下载主要的html / php / jsp / xyz页面,然后使用那里的cookie获取图像。

此代码也会为您处理URL。改编自http://wiki.apache.org/HttpComponents/QuickStart

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

/**
 * A example that demonstrates how HttpClient APIs can be used to perform
 * form-based logon.
 * based on 2008-2009 version 4 API http://hc.apache.org/
 */
public class ClientFormLogin {

public static void main(String[] args) throws Exception {

    DefaultHttpClient httpclient = new DefaultHttpClient();

    HttpGet httpget = new HttpGet("https://portal.sun.com/portal/dt");

    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();

    System.out.println("first page get: " + response.getStatusLine());
    if (entity != null) {
        entity.consumeContent();
    }
    System.out.println("Initial set of cookies:");
    List<Cookie> cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
        }
    }

    httpget = new HttpGet("<url-to-img>");


    response = httpclient.execute(httpget);


    System.out.println("Login form get: " + response.getStatusLine());
    if (entity != null) {
        byte[] data = HttpEntity.toByteArray(response);
//save to file
    }

    System.out.println("Post logon cookies:");
    cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
        }
    }
}
}

1
我正在回答我的问题,以表明我已经找到了问题所在。 在Eclipse中: 右键单击.class >属性 在资源中,更改编码设置。 我的是Cp1252。我改成了UTF-8,我的代码变得一团糟。我修复了它,现在它可以工作了。
你所有的答案实际上都是正确的,因为我必须将“空格”更改为“%20”和其他东西。 谢谢。

0
请注意,您的 URL 包含空格和一个 é 字符。例如,空格通常被编码为 %20。您需要对 URL 进行编码,以便正确使用它。
有关更多信息,请参见

更新

编码也会对你的http://进行编码,因此你需要对重要部分进行编码,即文件名称。尝试像这样做:

String strUrl = "http://leitor1.mangasproject.com/3e1e967e9b793e908f8eae83c74dba9bcccce6a5535b4b462bd9994537bfe15c/1c96b0ef48b44ff71102d96f7ac2b515a0b7be31d04d7420f3d133d923189953/";
strUtil.append(URLEncoder.encode("Hunter x Hunter 340 - 00 - créditos.jpg", "UTF-8"));
URL url = new URL(strUrl);
org.apache.commons.io.FileUtils.copyURLToFile(url, file);

这个例子使用 "UTF-8" 作为字符集编码,但还有其他支持的选项。可以尝试这里列出的任何标准字符集


好的,我尝试对URL进行编码,当我在屏幕上显示字符串时,URL已经被编码了。但是,代码仍然出现错误。 =[ - Killer
没有问题...http:// 被编码为 http%3A%2F%2F。我认为我只需要对文件名进行编码(Hunter x Hunter 340 - 00 - créditos),对吗? - Killer
@rodrigokiller 好的,那就采纳我提出的建议吧。让我拿一个例子来更新我的回答。 - Fritz
@rodrigokiller,已完成,请现在尝试。 - Fritz
由于这是一个简单的代码,你能告诉我这个代码是否在你那里工作吗?因为在这里它没有工作。可能是服务器不支持GET/POST请求吗?你的代码给了我这个:http://leitor1.mangasproject.com/3e1e967e9b793e908f8eae83c74dba9bcccce6a5535b4b462bd9994537bfe15c/1c96b0ef48b44ff71102d96f7ac2b515a0b7be31d04d7420f3d133d923189953/Hunter+x+Hunter+340+-+00+-+cr%E9ditos.jpg - Killer
显示剩余2条评论

0

URLEncoder 不是最好的解决方案。这段代码可以工作:

File file = new File("D:" + "//" + "Hunter x Hunter 340 - 00 - créditos.jpg");
URI uri = new URI ("http", "leitor1.mangasproject.com",
  "/3e1e967e9b793e908f8eae83c74dba9bcccce6a5535b4b462bd9994537bfe15c/1c96b0ef48b44ff71102d96f7ac2b515a0b7be31d04d7420f3d133d923189953/Hunter x Hunter 340 - 00 - créditos.jpg",
null  );

org.apache.commons.io.FileUtils.copyURLToFile(uri.toURL(), file);

在这里我使用java.net.URI类进行转义。请注意,我使用了URI类构造函数,其中我将"http"作为第一个参数传递,"leitor1.mangasproject.com"作为第二个参数,URL的其余部分作为第3个参数,并将null作为第4个参数


0

你可能需要对图像的URL进行URL编码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接