• 如何使用java语言下载https的网络文件


    1. import java.io.BufferedInputStream;
    2. import java.io.FileOutputStream;
    3. import java.io.IOException;
    4. import java.io.InputStream;
    5. import java.net.URL;
    6. import javax.net.ssl.HttpsURLConnection;
    7. public class HttpsDownloader {
    8. public static void main(String[] args) {
    9. String fileURL = "https://example.com/file.txt"; // 要下载的文件URL
    10. String savePath = "downloaded_file.txt"; // 下载后保存的文件路径
    11. try {
    12. URL url = new URL(fileURL);
    13. HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    14. connection.connect();
    15. int responseCode = connection.getResponseCode();
    16. if (responseCode == HttpsURLConnection.HTTP_OK) {
    17. InputStream inputStream = new BufferedInputStream(connection.getInputStream());
    18. FileOutputStream outputStream = new FileOutputStream(savePath);
    19. byte[] buffer = new byte[1024];
    20. int bytesRead;
    21. while ((bytesRead = inputStream.read(buffer)) != -1) {
    22. outputStream.write(buffer, 0, bytesRead);
    23. }
    24. outputStream.close();
    25. inputStream.close();
    26. System.out.println("文件下载完成!");
    27. } else {
    28. System.out.println("无法连接到文件URL。响应代码:" + responseCode);
    29. }
    30. } catch (IOException e) {
    31. e.printStackTrace();
    32. }
    33. }
    34. }

    上述代码首先创建一个URL对象,然后使用openConnection()方法获取到HttpsURLConnection对象。接下来,通过调用connect()方法建立与服务器的连接。

    如果连接成功(HTTP响应码为200),则可以从输入流中读取数据,并将其写入指定的文件输出流中。最后,关闭输入流和输出流,并打印出下载完成的消息。

    请将fileURL替换为您要下载的文件的真实URL,将savePath替换为下载文件的保存路径。运行代码后,您将在指定位置找到下载的文件。

    上面代码报错,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

    这个错误通常发生在尝试通过HTTPS下载文件时,因为服务器的证书无法验证或未被信任。解决方法是将服务器的证书添加到Java的信任存储库中。以下是一种简单的方法:

    1. 首先,从浏览器中导出服务器的证书(例如Chrome浏览器):

      • 在浏览器中打开目标URL。
      • 单击地址栏旁边的锁图标,并选择"证书"或"证书详细信息"。
      • 在证书详细信息窗口中,导航到"详细信息"选项卡。
      • 单击"复制到文件"按钮,并按照指示保存证书,选择DER格式。
    2. 使用keytool工具将证书添加到Java的信任存储库:

      • 打开命令提示符或终端。

      • 导航到Java的安装目录的bin目录下(例如:C:\Program Files\Java\jdk1.8.0_271\bin)。

      • 运行以下命令,将证书添加到信任存储库中(替换path/to/certificate.der为导出的证书路径和名称):

    keytool -importcert -alias mycert -keystore cacerts -file path/to/certificate.der

    您将被要求输入Java信任存储库的密码,默认情况下是changeit

    3.运行代码时,Java将能够验证服务器的证书并建立安全的HTTPS连接。

    如果您仍然遇到问题,请确保您的Java运行时环境是最新版本,并尝试使用更可靠的证书进行测试。

    如果不添加证书,如何绕过证书?

    1. import java.io.BufferedInputStream;
    2. import java.io.FileOutputStream;
    3. import java.io.IOException;
    4. import java.io.InputStream;
    5. import java.net.URL;
    6. import javax.net.ssl.HttpsURLConnection;
    7. import javax.net.ssl.SSLContext;
    8. import javax.net.ssl.TrustManager;
    9. import javax.net.ssl.X509TrustManager;
    10. import java.security.cert.CertificateException;
    11. import java.security.cert.X509Certificate;
    12. public class HttpsDownloader {
    13. public static void main(String[] args) {
    14. String fileURL = "https://example.com/file.txt"; // 要下载的文件URL
    15. String savePath = "downloaded_file.txt"; // 下载后保存的文件路径
    16. try {
    17. // 创建信任所有证书的 TrustManager
    18. TrustManager[] trustAllCerts = new TrustManager[]{
    19. new X509TrustManager() {
    20. public java.security.cert.X509Certificate[] getAcceptedIssuers() {
    21. return null;
    22. }
    23. public void checkClientTrusted(X509Certificate[] certs, String authType) {
    24. }
    25. public void checkServerTrusted(X509Certificate[] certs, String authType) {
    26. }
    27. }
    28. };
    29. // 设置 SSL 上下文
    30. SSLContext sslContext = SSLContext.getInstance("TLS");
    31. sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
    32. HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    33. URL url = new URL(fileURL);
    34. HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    35. connection.connect();
    36. int responseCode = connection.getResponseCode();
    37. if (responseCode == HttpsURLConnection.HTTP_OK) {
    38. InputStream inputStream = new BufferedInputStream(connection.getInputStream());
    39. FileOutputStream outputStream = new FileOutputStream(savePath);
    40. byte[] buffer = new byte[1024];
    41. int bytesRead;
    42. while ((bytesRead = inputStream.read(buffer)) != -1) {
    43. outputStream.write(buffer, 0, bytesRead);
    44. }
    45. outputStream.close();
    46. inputStream.close();
    47. System.out.println("文件下载完成!");
    48. } else {
    49. System.out.println("无法连接到文件URL。响应代码:" + responseCode);
    50. }
    51. } catch (Exception e) {
    52. e.printStackTrace();
    53. }
    54. }
    55. }

    这段代码创建了一个自定义的TrustManager,其中实现了checkClientTrusted()checkServerTrusted()方法,并且不会抛出异常或进行任何验证。然后,通过调用SSLContext.getInstance("TLS")得到一个SSLContext实例,并使用自定义的TrustManager初始化它。最后,调用HttpsURLConnection.setDefaultSSLSocketFactory()将自定义的SSL上下文设置为默认的SSLSocketFactory。

    请注意,这种绕过证书验证的方法会降低安全性,因为它允许信任所有证书,包括不受信任或伪造的证书。仅在测试环境中使用此方法,并确保在生产环境中使用有效且受信任的证书来确保安全性。

    上面的代码提示:javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present

    如果您在绕过证书验证时遇到javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present错误,这表示服务器的证书中缺少主体备用名称(Subject Alternative Names)。

    为了解决这个问题,您可以使用一个自定义的HostnameVerifier来跳过主体备用名称的验证。以下是修改后的示例代码:

    1. import java.io.BufferedInputStream;
    2. import java.io.FileOutputStream;
    3. import java.io.IOException;
    4. import java.io.InputStream;
    5. import java.net.URL;
    6. import javax.net.ssl.HttpsURLConnection;
    7. import javax.net.ssl.HostnameVerifier;
    8. import javax.net.ssl.SSLSession;
    9. public class HttpsDownloader {
    10. public static void main(String[] args) {
    11. String fileURL = "https://example.com/file.txt"; // 要下载的文件URL
    12. String savePath = "downloaded_file.txt"; // 下载后保存的文件路径
    13. try {
    14. // 创建信任所有证书的 TrustManager
    15. TrustManager[] trustAllCerts = new TrustManager[]{
    16. new X509TrustManager() {
    17. public java.security.cert.X509Certificate[] getAcceptedIssuers() {
    18. return null;
    19. }
    20. public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
    21. }
    22. public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
    23. }
    24. }
    25. };
    26. // 设置 SSL 上下文
    27. SSLContext sslContext = SSLContext.getInstance("TLS");
    28. sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
    29. HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    30. // 创建自定义 HostnameVerifier
    31. HostnameVerifier allHostsValid = new HostnameVerifier() {
    32. public boolean verify(String hostname, SSLSession session) {
    33. return true;
    34. }
    35. };
    36. HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    37. URL url = new URL(fileURL);
    38. HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    39. connection.connect();
    40. int responseCode = connection.getResponseCode();
    41. if (responseCode == HttpsURLConnection.HTTP_OK) {
    42. InputStream inputStream = new BufferedInputStream(connection.getInputStream());
    43. FileOutputStream outputStream = new FileOutputStream(savePath);
    44. byte[] buffer = new byte[1024];
    45. int bytesRead;
    46. while ((bytesRead = inputStream.read(buffer)) != -1) {
    47. outputStream.write(buffer, 0, bytesRead);
    48. }
    49. outputStream.close();
    50. inputStream.close();
    51. System.out.println("文件下载完成!");
    52. } else {
    53. System.out.println("无法连接到文件URL。响应代码:" + responseCode);
    54. }
    55. } catch (Exception e) {
    56. e.printStackTrace();
    57. }
    58. }
    59. }

    以上代码除了添加自定义的HostnameVerifier外,其他部分与之前的代码相同。使用这个自定义的HostnameVerifier,将绕过对主体备用名称的验证,并允许连接到没有主体备用名称的服务器。

    请注意,绕过主体备用名称的验证也会降低安全性,因为它可能容易受到中间人攻击。仅在测试环境中使用此方法,并在生产环境中使用正确配置的证书和合适的验证机制以确保安全性。

  • 相关阅读:
    51单片机学习:红外遥控实验
    PCB设计笔记
    Node的api
    QT中启动窗口QSplashScreen的使用
    每日三问-前端(第二十期)
    minigui编译移植
    [附源码]Python计算机毕业设计Django四川景区管理系统
    【Vue3教程】创建你的第一个Vue 3项目
    redis 键常用命令
    vue2与vue3的使用区别与组件通信
  • 原文地址:https://blog.csdn.net/suya2011/article/details/134424819