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()
代替该样板)



