gpt4 book ai didi

java - 文件下载 SSLHandshakeException

转载 作者:可可西里 更新时间:2023-11-01 16:32:39 26 4
gpt4 key购买 nike

当我尝试使用 Java 代码以编程方式下载文件时,出现异常:

Exception in thread "main" 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(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1513)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at java.net.URL.openStream(URL.java:1045)
at DownloadFileExample.download(DownloadFileExample.java:15)
at DownloadFileExample.main(DownloadFileExample.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
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(PKIXValidator.java:387)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
at sun.security.validator.Validator.validate(Validator.java:260)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491)
... 20 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
... 26 more

这是我的代码,包括我要下载的文件:

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class DownloadFileExample
{
public static void download(String downloadURL) throws IOException
{
URL website = new URL(downloadURL);
String fileName = "downloaded.zip";

try (InputStream inputStream = website.openStream())
{
Files.copy(inputStream, Paths.get(fileName), StandardCopyOption.REPLACE_EXISTING);
}
}

public static void main(String[] arguments) throws IOException
{
String downloadURL = "https://mh-nexus.de/downloads/HxDSetupEN.zip";
download(downloadURL);
}
}

我一开始尝试使用 HTTP 协议(protocol)下载,但它给了我一个 301 moved permanently 状态码,所以我意识到它重定向到 HTTPS。但是,HTTPS 给了我上面的 SSLHandshakeException。使用浏览器,下载工作完美。如何使用 Java 代码正确下载文件?

最佳答案

发生这种情况是因为证书 https://mh-nexus.de网站不在你的JRE白名单中

选项

1) 在JRE白名单中包含服务器证书 (jre/lib/security/cacerts)要下载服务器证书,请使用浏览器打开站点,右键单击绿色锁,选择“查看证书”并下载

探索 cacerts 和导入可信证书的最简单方法是使用 GUI 工具,如 portecle (http://portecle.sourceforge.net/)。你也可以使用 keytool

keytool -import -trustcacerts -keystore /opt/java/jre/lib/security/cacerts -alias mycert -noprompt -storepass changeit -file /tmp/examplecert.crt

参见 How to properly import a selfsigned certificate into Java keystore that is available to all Java applications by default?

2) 使用您自己的信任库并包含服务器证书

System.setProperty ("javax.net.ssl.trustStore" path_to_your_cacerts_file);

3) 根本不要使用信任库(BullyWiiPlaza 回应)

关于java - 文件下载 SSLHandshakeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37562926/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com