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

4

6 回答 6

2

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

于 2013-08-13T19:01:41.197 回答
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());
        }
    }
}
}
于 2013-08-13T20:54:57.223 回答
1

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

您所有的答案实际上都是正确的,因为我必须将“空格”更改为“%20”和其他内容。谢谢你。

于 2013-08-14T03:36:25.937 回答
0

Note that your url contains whitespaces and an é. As an example, whitespaces are usually encoded as %20. You'll need to encode your URL in order to use it properly.

For more information see


Update

The encoding will also encode your http://, so you need to encode the important part, a.k.a the name of the file. Try something like this:

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);

This example uses "UTF-8" as the charset encoding, but there are other supported options. Try any of the Standard Charsets listed here.

于 2013-08-13T19:03:17.187 回答
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 的其余部分作为第三个参数和 null 作为第四个参数

于 2013-08-13T19:41:58.023 回答
0

您可能需要对图像的 url 进行 url 编码。

于 2013-08-13T19:00:37.483 回答