当前位置: 首页 > 知识库问答 >
问题:

文件下载SSLHandshakeException

斜昊空
2023-03-14

当我尝试使用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协议下载,但它给我一个301移动永久状态代码,因此我意识到它重定向到HTTPS。但是,https从上面给出了SSLHandShakeException。使用浏览器,下载工作完美无缺。如何使用Java代码正确下载文件?

共有1个答案

计泉
2023-03-14

这是因为证书https://mh-nexus.de网站不在JRE的白名单上

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

探索cacerts并导入受信任证书的最简单方法是使用portecle(http://portecle.sourceforge.net/)这样的GUI工具。也可以使用keytool

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

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

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

3)完全不使用信任库(BullyWiiPlaza响应)

 类似资料:
  • 下载 <?php /** * 下载文件请求示例 */ require dirname(__DIR__) . '/vendor/autoload.php'; use Yurun\Util\HttpRequest; use Yurun\Util\YurunHttp; use Yurun\Util\YurunHttp\Handler\Swoole; $url = 'http://www.baid

  • 问题内容: 我正在尝试使用Flask创建一个Web应用程序,该应用程序允许用户上传文件并将其提供给其他用户。现在,我可以将文件正确地上传到upload_folder了。但是我似乎找不到找到让用户下载回来的方法。 我将文件名的名称存储到数据库中。 我有一个为数据库对象服务的视图。我也可以删除它们。 在我的HTML中,我有: 和下载视图: 但它正在返回: 未找到 在服务器上找不到请求的URL。如果您手

  • 问题内容: 我是使用Django的新手,我正在尝试开发一个网站,用户可以在其中上传许多excel文件,然后将这些文件存储在媒体文件夹Webproject / project / media中。 然后,该文档会与它们上载的任何其他文档一起显示在列表中,你可以单击这些文档,它会显示有关它们的基本信息以及他们上载的excelfile的名称。从这里,我希望能够使用链接再次下载相同的excel文件: 我的网

  • 问题内容: 我正在使用简单的文件下载脚本: 它在最大200mb的本地服务器上运行。 当我在我的网站上尝试此代码时,它将下载173KB而不是200MB的文件。 我检查了所有内容,编写了一些自定义代码(使用ob函数和fread而非readfile),但无法下载大文件。 谢谢您的回答。 我正在使用Apache 2.2,PHP 5.3 可以处理所有大文件的所有PHP设置。(执行时间,内存限制,… 问题答案

  • 我有一个文件表示为块的列表,目标是下载所有块,加入并保存为一个文件。 它应该适用于大文件 应该是跨浏览器解决方案 null null null null 但我仍然无法实现我的目标与涵盖的需求… 如果有人有经验,最好的解决方案,我恳请分享它在这里。谢谢

  • 我试图触发文件下载,我有问题这样做Safari(火狐 这是我的客户代码: Safari响应在屏幕上打印出来(加载在同一页上)。 注意:我尝试过其他建议的方法,但每个方法都有自己的问题。 更新:在响应标头中,我看到内容处理被设置为内联而不是附件。为什么会发生这种事? 我做错了什么?