找回密码
 立即注册
securityUnknownSourcesun | 软件设计/软件工程 2022-05-04 236 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
问题
我必须制作一个显示药包图像的应用程序。我发现这个网站有一些图片,但我尝试用Java语言的小程序下载可用的图片,但失败了。

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

有办法吗?

编辑:代码和错误
  1. public class DescargarArchivo {

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

  5.     String folder = "downloads/";

  6.     File dir = new File(folder);

  7.     if (!dir.exists())
  8.             if (!dir.mkdir())
  9.                     return;

  10.     File file = new File(folder + name);

  11.     try {

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

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

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

  21.             int b = 0;

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

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

  27.             out.close();
  28.             in.close();

  29.             System.out.println("\ncomplete download\n");
  30.     } catch (MalformedURLException e) {
  31.             System.out.println("url: " + url + " invalid");
  32.     } catch (IOException e) {
  33.             e.printStackTrace();
  34.     }
  35. }
  36. }

  37. 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
  38. at sun.security.ssl.Alerts.getSSLException(Unknown Source)
  39. at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
  40. at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
  41. at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
  42. at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
  43. at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
  44. at sun.security.ssl.Handshaker.processLoop(Unknown Source)
  45. at sun.security.ssl.Handshaker.process_record(Unknown Source)
  46. at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
  47. at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
  48. at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
  49. at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
  50. at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
  51. at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
  52. at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source)
  53. at com.test.java.net.DescargarArchivo.main(DescargarArchivo.java:34)
  54. Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
  55.     at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
  56.     at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
  57.     at sun.security.validator.Validator.validate(Unknown Source)
  58.     at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
  59.     at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
  60.     at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
  61.     ... 12 more
  62. Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
  63.     at sun.security.provider.certpath.SunCertPathBuilder.build(Unknown Source)
  64.     at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
  65.     at java.security.cert.CertPathBuilder.build(Unknown Source)
  66.     ... 18 more
复制代码

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





上一篇:Java中的字符序列VS字符串?
下一篇:将数据库从docker备份到real machine