Reputation: 85
I have a simple code I use to download images using its url, my code works well but for some reason I don’t understand why I can’t download the following image: http://www.plazavea.com.pe/RepositorioAPS/0/0/cat/37/FOLLETO23_CLIENTE10.jpg If I try to download another image it works well (for example: http://www.soyunalbondiga.com/wp-content/uploads/2013/05/obiwan.jpg). Additionally, I saw that when I run my program with that url(the bad one) It printed that the ContentType is html/text, but when I put this url in a browser it shows the image without problems. I necessary need to download the images from that domain (www.plazavea.com.pe). Please your help.
public static void main(String[] args) {
try {
// Url con la foto
URL url = new URL(
"http://plazavea.com.pe/RepositorioAPS/0/0/cat/37/FOLLETO23_CLIENTE10.jpg");
// establecemos conexion
URLConnection urlCon = url.openConnection();
// Sacamos por pantalla el tipo de fichero
System.out.println(urlCon.getContentType());
// Se obtiene el inputStream de la foto web y se abre el fichero
// local.
InputStream is = urlCon.getInputStream();
FileOutputStream fos = new FileOutputStream("d:/foto.jpg");
// Lectura de la foto de la web y escritura en fichero local
byte[] array = new byte[1000]; // buffer temporal de lectura.
int leido = is.read(array);
while (leido > 0) {
fos.write(array, 0, leido);
leido = is.read(array);
}
// cierre de conexion y fichero.
is.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Thanks for your help.
Carlos.
Upvotes: 0
Views: 532
Reputation: 2211
Seems like plazavea.com.pe is cheking the user-agent.
You have to set a different user-agent for your java application. You need to use this before creating the URLConnection object:
System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
So, your code would be like this:
public static void main(String[] args) {
try {
// Url con la foto
URL url = new URL(
"http://plazavea.com.pe/RepositorioAPS/0/0/cat/37/FOLLETO23_CLIENTE10.jpg");
// establecemos user-agent del sistema
System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
// establecemos conexion
URLConnection urlCon = url.openConnection();
// Sacamos por pantalla el tipo de fichero
System.out.println(urlCon.getContentType());
// Se obtiene el inputStream de la foto web y se abre el fichero
// local.
InputStream is = urlCon.getInputStream();
FileOutputStream fos = new FileOutputStream("d:/foto.jpg");
// Lectura de la foto de la web y escritura en fichero local
byte[] array = new byte[1000]; // buffer temporal de lectura.
int leido = is.read(array);
while (leido > 0) {
fos.write(array, 0, leido);
leido = is.read(array);
}
// cierre de conexion y fichero.
is.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Tested and working
Some brief explanation:
The user-agent is just an identificator.
The application which requests a webpage/image/etc identifies itself to the server (for example, the server can serve a different webpage to a mobile device and to your desktop computer).
If nothing said, java will identify itself as something similar to "Java/1.6.0_04".
For some reason, the creator of plazavea.com.pe decided that the website won't serve images to whoever identifies himself as "Java/something".
With "System.setProperty("http.agent", "something")" you can just make your application identify with any user-agent you want.
Upvotes: 2
Reputation: 8338
That is because you are not only downloading the picture, you are downloading also the page in which the image is.
Run this program and you'll see HTML instead of the image bits:
public static void main(String[] args) {
try {
// Url con la foto
URL url = new URL("http://plazavea.com.pe/RepositorioAPS/0/0/cat/37/FOLLETO23_CLIENTE10.jpg");
// establecemos conexion
URLConnection urlCon = url.openConnection();
// Sacamos por pantalla el tipo de fichero
System.out.println(urlCon.getContentType());
// Se obtiene el inputStream de la foto web y se abre el fichero
// local.
InputStream is = urlCon.getInputStream();
// Lectura de la foto de la web y escritura en fichero local
byte[] array = new byte[1000]; // buffer temporal de lectura.
int leido = is.read(array);
while (leido > 0) {
leido = is.read(array);
System.out.println(new String(array));
}
// cierre de conexion y fichero.
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 0