3

Here is the code.

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

I'm getting this error when running this code:

    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)

I tried to use

InputStream in = new BufferedInputStream(url.openStream()); 

but this didnt work too.

Edit: I must say now that in NetBeans all the codes works fine, but, im coding in Eclipse. :( I must say too that most of the files of this host works fine in the Eclipse.

Killer
  • 188
  • 3
  • 14
  • 1
    If you enter your path in the browser, it encodes it to `http://leitor1.mangasproject.com/3e1e967e9b793e908f8eae83c74dba9bcccce6a5535b4b462bd9994537bfe15c/1c96b0ef48b44ff71102d96f7ac2b515a0b7be31d04d7420f3d133d923189953/Hunter%20x%20Hunter%20340%20-%2000%20-%20cr%C3%A9ditos.jpg` before sending the request. I don't know what encoding that is, but if you shove that path in your Java code, it will work. – Sotirios Delimanolis Aug 13 '13 at 19:17

6 Answers6

2

Try passing in URLEncoder.encode(IMAGE_URL, "UTF-8") to the URL as opposed to just the plain image URL.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Josh M
  • 11,611
  • 7
  • 39
  • 49
  • Ok, I'll try that. Probably it's because of the file name (é). – Killer Aug 13 '13 at 19:05
  • Hum, i tried that and with a sout (syso), i can see the URL encoded. But, when the inputStream open the stream, the error happens again. – Killer Aug 13 '13 at 19:15
1

Might be that the site does not like file leaching. To get aound that - if you have permission to use the site like this you can use HttpComponents from apache to first navigate to home page or page contraining the image. Do not need to download all the js and css just the main html/ php / jsp / xyz page and then use the cookies from there to get the image.

This code will look after the URL for you too. Adapted from 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());
        }
    }
}
}
Killer
  • 188
  • 3
  • 14
tgkprog
  • 4,493
  • 4
  • 41
  • 70
1

I'm answering my question to say that i found the issue. In Eclipse: Right-click in the .class > Propertie In Resource, change the Encoding settings. Mine was Cp1252. I changed to UTF-8, my code turned a mess. I fixed it and now its working.

All your answers are in fact correctly, cause I must change the "spaces" to "%20" and others things. Thank you.

Killer
  • 188
  • 3
  • 14
0

You probably need to url encode the url to the image.

Joseph Helfert
  • 421
  • 3
  • 13
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.

Community
  • 1
  • 1
Fritz
  • 9,987
  • 4
  • 30
  • 49
  • Ok, i tried to encode the URL and when i show in the screen the String, the URL is encoded. But, the code still getting the error. =[ – Killer Aug 13 '13 at 19:16
  • @rodrigokiller Copy an paste the encoded URL in your browser, do you see the image? – Fritz Aug 13 '13 at 19:18
  • no... the http:// is encoded (changed) to http%3A%2F%2F. I think that I need to encode only the filename (the Hunter x Hunter 340 - 00 - créditos) right? – Killer Aug 13 '13 at 19:24
  • @rodrigokiller OK, then take the hint in the answer I suggested. Let me put it as an example and update my answer. – Fritz Aug 13 '13 at 19:25
  • @rodrigokiller Done, try it out now. – Fritz Aug 13 '13 at 19:28
  • As this is a simple code, can you say me if with you this works? Because here didnt work. Can be something like the server doesnt gives suport to GET/POST Requests? Your code give me that: _http://leitor1.mangasproject.com/3e1e967e9b793e908f8eae83c74dba9bcccce6a5535b4b462bd9994537bfe15c/1c96b0ef48b44ff71102d96f7ac2b515a0b7be31d04d7420f3d133d923189953/Hunter+x+Hunter+340+-+00+-+cr%E9ditos.jpg_ – Killer Aug 13 '13 at 19:38
  • @rodrigokiller Or, maybe your server doesn't understand "UTF-8". Try with any of the other charsets I point at in my (edited) answer. – Fritz Aug 14 '13 at 14:44
0

URLEncoder doesn't look like the best solution. This code works:

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

I'm using the java.net.URI class here to do the escaping. Note that I'm using the URI class constructor where I pass "http" as first argument, "leitor1.mangasproject.com" as second argument, the rest of the URL as 3rd argument and null as 4th argument

Luciano Fiandesio
  • 10,037
  • 10
  • 48
  • 56