52ky 发表于 2022-5-4 13:43:27

从 Java 中的 HTTPS URL 下载图像

问题
我必须制作一个显示药包图像的应用程序。我发现这个网站有一些图片,但我尝试用Java语言的小程序下载可用的图片,但失败了。

我认为 HTTPS 是导致问题的原因。

有办法吗?

编辑:代码和错误
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

回答
您需要设置权限以允许服务器证书。在这里它解释了如何设置它,以便所有证书都是可信的。



页: [1]
查看完整版本: 从 Java 中的 HTTPS URL 下载图像