【发布时间】:2013-09-05 17:32:40
【问题描述】:
我正在尝试使用输出流从 URL 保存文件。该 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();
}
由于出现了错误,因此在专用的 floder 中没有发生任何事情。如何从 HTTPS url 获取文件?
【问题讨论】:
标签: java https httpurlconnection outputstream filepicker.io