我能';t使用java代码下载特定图像

我能';t使用java代码下载特定图像,java,Java,我有一个简单的代码,我使用它的url下载图像,我的代码工作得很好,但由于某些原因,我不明白为什么我不能下载以下图像: 如果我尝试下载另一个图像,效果会很好(例如:)。 此外,我看到,当我使用该url(错误的url)运行程序时,它打印的内容类型是html/文本,但当我将该url放在浏览器中时,它会毫无问题地显示图像。 我需要从那个域名下载图片(www.plazavea.com.pe)。请帮忙 public static void main(String[] args) { try {

我有一个简单的代码,我使用它的url下载图像,我的代码工作得很好,但由于某些原因,我不明白为什么我不能下载以下图像: 如果我尝试下载另一个图像,效果会很好(例如:)。 此外,我看到,当我使用该url(错误的url)运行程序时,它打印的内容类型是html/文本,但当我将该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();
    }

}
谢谢你的帮助


Carlos。

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

运行此程序,您将看到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();
    }
}

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

}
测试和工作

一些简短的解释:

用户代理只是一个标识器

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

如果什么都不说,java将把自己标识为类似于“java/1.6.0_04”的东西

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


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

您在文件中写了什么?嗨,Brett,我只想下载此图像:使用java程序。谢谢。所以你下载的内容是html/文本并写入文件,文件的内容是什么?谢谢,对我有用。我将搜索有关用户代理的更多信息。@user3758346,如果它解决了您的问题,您也可以接受这个答案。顺便说一下,为了完整性,我刚刚添加了一个简短的解释。