繁体   English   中英

如何从JAVA中的HTTPS URL保存文件?

[英]How to save the file from HTTPS url in JAVA?

我正在尝试使用outputstream从URL保存文件。 该网址由https保护。 所以当我尝试获取文件时出现以下错误

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 java.net.URL.openStream(Unknown Source) 
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
    at java.security.cert.CertPathBuilder.build(Unknown Source)
    ... 60 more

假设我要从此URL打开文件

https://www.filepicker.io/api/file/KW9EJhYtS6y48Whm2S6D?signature=4098f262b9dba23e4766ce127353aaf4f37fde0fd726d164d944e031fd862c18&policy=eyJoYW5kbGUiOiJLVzlFSmhZdFM2eTQ4V2htMlM2RCIsImV4cGlyeSI6MTUwODE0MTUwNH0=

所以我做类似的事情:

try{    
    URL URL = new URL('https://www.filepicker.io/api/file/KW9EJhYtS6y48Whm2S6D?signature=4098f262b9dba23e4766ce127353aaf4f37fde0fd726d164d944e031fd862c18&policy=eyJoYW5kbGUiOiJLVzlFSmhZdFM2eTQ4V2htMlM2RCIsImV4cGlyeSI6MTUwODE0MTUwNH0=');
    String = path = "D://download/";
    InputStream ins = url.openStream();
    OutputStream ous = new FileOutputStream(path);
    final byte[] b = new byte[2048];
    int length;

        while ((length = inputStream.read(b)) != -1) {
               ous.write(b, 0, length);
         }

           ins.close();
           ous.close();
}

结果不会在专用选粉机上发生任何事情,因为会显示错误。 如何从HTTPS网址获取文件?

HTTPS连接需要握手。 即明确承认对方。 服务器已经通过HTTPS证书标识了自己,但是您显然没有在信任存储区中使用此证书,并且您在Java代码中没有任何位置明确确认该标识,因此HttpsURLConnection (此处将在此处使用)拒绝继续HTTPS请求。

作为开始的示例,您可以在类中使用以下代码,以使HttpsURLConnection接受所有SSL证书,而不管您使用的是HTTPS URL。

static {
    final TrustManager[] trustAllCertificates = new TrustManager[] {
        new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null; // Not relevant.
            }
            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
                // Do nothing. Just allow them all.
            }
            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
                // Do nothing. Just allow them all.
            }
        }
    };

    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCertificates, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (GeneralSecurityException e) {
        throw new ExceptionInInitializerError(e);
    }
}

但是,如果您希望在每个证书的基础上进行更细粒度的控制,请根据其Javadoc相应地实现这些方法。


具体问题无关 ,代码中还有第二个问题。 您正在尝试将下载的文件保存为文件夹而不是文件。

String = path = "D://download/";
OutputStream ous = new FileOutputStream(path);

除了语法错误(很可能是在编写问题时粗心大意导致的错误(即直接编辑有问题的代码而不是实际复制粘贴工作代码))之外,这没有任何意义。 您不应将文件夹指定为保存位置。 您应该指定一个文件名。 您可以根据需要从Content-Disposition标头中提取它,或使用File#createTempFile()自动生成一个标头。 例如

File file = File.createTempFile("test-", ".jpg", new File("D:/download/"));
Files.copy(url.openStream(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);

(如果您已经在使用Java 7,则只需使用Files#copy()而不是该样板即可)

错误的原因是Java无法确认证书的有效性。 可以通过将证书导入到JAVA证书存储中来解决此问题,也可以通过禁用SSL证书验证来“修复”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM