【问题标题】:I can't download a specific image using java code我无法使用 java 代码下载特定图像
【发布时间】:2014-06-19 23:50:26
【问题描述】:

我有一个简单的代码,我用它的 url 来下载图像,我的代码运行良好,但由于某种原因,我不明白为什么我不能下载以下图像: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();
    }

}

感谢您的帮助。

卡洛斯。

【问题讨论】:

  • 你在文件中写了什么?
  • 嗨,Brett,我只想下载这张图片:plazavea.com.pe/RepositorioAPS/0/0/cat/37/… 使用 java 程序。谢谢。
  • 那么您下载的内容是 html/text 并写入文件,该文件的内容是什么?

标签: java


【解决方案1】:

好像 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")" 您可以让您的应用程序识别您想要的任何用户代理。

【讨论】:

  • 感谢它对我有用。我将搜索有关用户代理的更多信息。
  • @user3758346,如果它解决了您的问题,您也可以接受这个答案。
  • 顺便说一下,为了完整起见,我只是添加了一个简短的解释。
【解决方案2】:

那是因为你不仅在下载图片,还在下载图片所在的页面。

运行这个程序,你会看到 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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    相关资源
    最近更新 更多