I can't download a specific image using java code -
I can't download a specific image using java code -
i have simple code utilize download images using url, code works reason don’t understand why can’t download next image: http://www.plazavea.com.pe/repositorioaps/0/0/cat/37/folleto23_cliente10.jpg if seek download image works (for example: http://www.soyunalbondiga.com/wp-content/uploads/2013/05/obiwan.jpg). additionally, saw when run programme url(the bad one) printed contenttype html/text, when set url in browser shows image without problems. necessary need download images domain (www.plazavea.com.pe). please help.
public static void main(string[] args) { seek { // 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 = 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(); } grab (exception e) { e.printstacktrace(); } }
thanks help.
carlos.
seems plazavea.com.pe cheking user-agent.
you have set different user-agent java application. need utilize before creating urlconnection object:
system.setproperty("http.agent", "mozilla/5.0 (windows nt 5.1; rv:19.0) gecko/20100101 firefox/19.0");
so, code this:
public static void main(string[] args) { seek { // 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 = 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(); } grab (exception e) { e.printstacktrace(); } }
tested , working
some brief explanation:
the user-agent identificator.
the application requests webpage/image/etc identifies server (for example, server can serve different webpage mobile device , desktop computer).
if nil said, java identify similar "java/1.6.0_04".
for reason, creator of plazavea.com.pe decided website won't serve images whoever identifies himself "java/something".
with "system.setproperty("http.agent", "something")" can create application identify user-agent want.
java
Comments
Post a Comment