I have a java program that tries to download a file from an ip address. Let's say the IP address is 10.2.14.35. So, the file is located at https://10.2.14.35/path/to/the/file/myfile.txt. I have the following function which is intended to download that file:
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());
}
}
And using the function as:
downloadFile(" https://10.2.14.35/path/to/the/file/myfile.txt", "/tmp/myfile.txt")
I get the error java.security.cert.CertificateException: No subject alternative names matching IP address 10.2.14.35 found
. I found some questions were asked on 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?) but none of them worked (or maybe I was doing something wrong somewhere.)
From the browser, I tried to download the file and it got downloaded. However, it was showing that the site is not secure because the certificate is invalid. I am not sure if the failure to download the file via the java program is because of this. On reading, I came across the concept of cacerts and that I have to make JRE understand that it is okay to connect to 10.2.14.35. For that, I need to create certificate and import it to cacerts. I did that but it did not work because maybe I missed something. Could someone help me in pointing out the mistake?