【发布时间】:2019-09-23 12:15:48
【问题描述】:
我有一个 java 程序试图从一个 IP 地址下载一个文件。假设 IP 地址是 10.2.14.35。因此,该文件位于https://10.2.14.35/path/to/the/file/myfile.txt。我有以下用于下载该文件的功能:
public static void downloadFile(String uri, String localFileName) {
try {
URL url = new URL(uri);
ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(localFileName);
fileOutputStream
.getChannel()
.transferFrom(readableByteChannel, 0 /* position */, Long.MAX_VALUE /* count */);
fileOutputStream.close();
readableByteChannel.close();
} catch (IOException e) {
throw new AgentException(ErrorProto.Type.kIOError, e.getMessage());
}
}
并使用函数为:
downloadFile(" https://10.2.14.35/path/to/the/file/myfile.txt", "/tmp/myfile.txt")
我收到错误 java.security.cert.CertificateException: No subject alternative names matching IP address 10.2.14.35 found。我发现有人在 SO(JAVA - No subject alternative names matching IP address、SSLHandshakeException: No subject alternative names present、How are SSL certificate server names resolved/Can I add alternative names using keytool?)上提出了一些问题,但没有一个有效(或者我在某处做错了什么。)
从浏览器中,我尝试下载文件并下载。但是,这表明该站点不安全,因为证书无效。我不确定通过java程序下载文件失败是否是因为这个。在阅读时,我遇到了 cacerts 的概念,我必须让 JRE 明白连接到 10.2.14.35 是可以的。为此,我需要创建证书并将其导入 cacerts。我这样做了,但它没有用,因为也许我错过了一些东西。有人可以帮我指出错误吗?
【问题讨论】: