繁体   English   中英

尝试从Web下载图像时出现FileNotFoundException

[英]FileNotFoundException when trying to download a image from web

这是代码。

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中工作正常。

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

可能是该网站不喜欢文件浸出。 为了得到这一点 - 如果你有权使用这样的网站,你可以使用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());
        }
    }
}
}

我正在回答我的问题,说我发现了这个问题。 在Eclipse中:右键单击.class> Propertie In Resource,更改编码设置。 我的是Cp1252。 我改为UTF-8,我的代码变得一团糟。 我修好了,现在还在工作。

你的所有答案都是正确的,因为我必须将“空格”改为“%20”和其他东西。 谢谢。

您可能需要将url编码为图像的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”作为charset编码,但还有其他支持的选项。 试试这里列出的任何标准字符集

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"作为第二个参数"leitor1.mangasproject.com" ,将URL的其余部分作为第三个参数"leitor1.mangasproject.com" ,将null作为第四个参数传递

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM