从Java下载HTTPS URL中的图像 [英] Download images from a HTTPS URL in Java

查看:129
本文介绍了从Java下载HTTPS URL中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做一个显示药物包装图像的应用程序。我发现这个网站有一些,但我正在尝试使用Java中的一个小程序下载可用的图像但是失败了。
我认为HTTPS会导致问题。

I have to do an application that shows an image of the medication packages. I've found this site that have some, but I'm trying to download the available images with a little program in Java but fails. I think HTTPS causes the issue.

有办法吗?

编辑:代码和错误

public class DescargarArchivo {

public static void main(String[] args) {
    String url = "https://medicamentos.sanidadmadrid.org/comun/visorCaratulas.aspx?cod=672629";
    String name = "test.jpg";

    String folder = "downloads/";

    File dir = new File(folder);

    if (!dir.exists())
            if (!dir.mkdir())
                    return;

    File file = new File(folder + name);

    try {

            URLConnection conn = new URL(url).openConnection();
            conn.connect();

            System.out.println("\ndownload: \n");
            System.out.println(">> URL: " + url);
            System.out.println(">> Name: " + name);
            System.out.println(">> size: " + conn.getContentLength()
                            + " bytes");

            InputStream in = conn.getInputStream();
            OutputStream out = new FileOutputStream(file);

            int b = 0;

            while (b != -1) {
                    b = in.read();

                    if (b != -1)
                            out.write(b);
            }

            out.close();
            in.close();

            System.out.println("\ncomplete download\n");
    } catch (MalformedURLException e) {
            System.out.println("url: " + url + " invalid");
    } catch (IOException e) {
            e.printStackTrace();
    }
}
}

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
at sun.security.ssl.Handshaker.processLoop(Unknown Source)
at sun.security.ssl.Handshaker.process_record(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source)
at com.test.java.net.DescargarArchivo.main(DescargarArchivo.java:34)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
    at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
    at sun.security.validator.Validator.validate(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
    ... 12 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.build(Unknown Source)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
    at java.security.cert.CertPathBuilder.build(Unknown Source)
    ... 18 more


推荐答案

您需要设置允许服务器证书的权限。 此处它解释了如何设置以便所有证书值得信赖。

You need to set the permission to allow the server certificate. Here it explains how to set so that all cert are trusted.

这篇关于从Java下载HTTPS URL中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆