我无法使用java代码下载特定图像

时间:2014-06-19 23:50:27

标签: java

我有一个简单的代码用于使用其网址下载图片,我的代码运行良好但由于某种原因我不明白为什么我无法下载以下图片:http://www.plazavea.com.pe/RepositorioAPS/0/0/cat/37/FOLLETO23_CLIENTE10.jpg 如果我尝试下载其他图像,则效果很好(例如:http://www.soyunalbondiga.com/wp-content/uploads/2013/05/obiwan.jpg)。 另外,我看到当我使用该URL运行我的程序时(坏的)它打印出ContentType是html / text,但是当我将这个url放在浏览器中时它显示图像没有问题。 我需要从该域名下载图像(www.plazavea.com.pe)。请你的帮助。

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

}

感谢您的帮助。

卡洛斯。

2 个答案:

答案 0 :(得分:2)

似乎plazavea.com.pe正在检查用户代理。

您必须为Java应用程序设置不同的用户代理。 您需要使用之前创建URLConnection对象:

System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");

所以,你的代码就像这样:

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

}

经过测试和运作

一些简短的解释:

用户代理只是一个识别器。

请求网页/图像/等的应用程序向服务器标识自己(例如,服务器可以为移动设备和台式计算机提供不同的网页)。

如果没有说,java会将自己标识为类似于“Java / 1.6.0_04”的东西。

出于某种原因,plazavea.com.pe的创建者决定该网站不会向自称为“Java / something”的人提供图像。

使用“ System.setProperty(”http.agent“,”something“)”您可以使您的应用程序识别您想要的任何用户代理。

答案 1 :(得分:0)

这是因为您不仅要下载图片,还要下载图片所在的页面。

运行此程序,您将看到HTML而不是图像位:

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