Reputation: 188
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.
Upvotes: 3
Views: 2897
Reputation: 10045
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
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.
Upvotes: 0
Reputation: 188
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.
Upvotes: 1
Reputation: 4598
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());
}
}
}
}
Upvotes: 1
Reputation: 10207
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
Upvotes: 0
Reputation: 11939
Try passing in URLEncoder.encode(IMAGE_URL, "UTF-8")
to the URL as opposed to just the plain image URL.
Upvotes: 2