• java访问https链接下载图片


    java访问https链接下载图片

    一、通过maven引入https工具包

    <dependency>
          <groupId>org.apache.httpcomponentsgroupId>
          <artifactId>httpclientartifactId>
          <version>4.5.13version>
        dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    二、https链接下载文件工具类

    package com.mhx.info.service;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.conn.ssl.NoopHostnameVerifier;
    import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
    import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.ssl.SSLContexts;
    import org.junit.Test;
    
    import java.io.FileOutputStream;
    import java.io.InputStream;
    
    
    /**
     * @Description: https文件链接下载文件
     * @BelongsProject: mhxFileDownload
     * @BelongsPackage: com.mhx.info.service
     * @ClassName: BatchDownloadFile
     * @Author: MHX
     * @CreateTime: 2022/11/25
     */
    public class BatchDownloadFileTest {
    
        /**
         * https来获得
         *
         * @throws Exception 异常
         */
        @Test
        public void httpsToGet() throws Exception {
    //        文件下载存储路径
            String savePath = "D:/zhxcmfs/myFiles";
    //        文件命名
            String fileName = "图片.png";
    //        https文件下载链接
            String apiHttp = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Flmg.jj20.com%2Fup%2Fallimg%2F1114%2F040221103339%2F210402103339-8-1200.jpg&refer=http%3A%2F%2Flmg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1671956738&t=7369439221c4fff3114c8cbaa28b4330";
    //        忽略对服务器端证书的校验
            SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(),
                    NoopHostnameVerifier.INSTANCE);
            CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(scsf).build();
            HttpGet httpget = new HttpGet(apiHttp);
            HttpResponse response = client.execute(httpget);
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
    //        对存储空间大小预定义
            int cache = 10 * 1024;
    //        文件输出路径
            FileOutputStream fileout = new FileOutputStream(savePath + "/" + fileName);
            byte[] buffer = new byte[cache];
            int ch = 0;
            while ((ch = is.read(buffer)) != -1) {
                fileout.write(buffer, 0, ch);
            }
            is.close();
            fileout.flush();
            fileout.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    三、https链接下载文件工具类讲解

    // 文件下载存储路径
    String savePath = “D:/zhxcmfs/myFiles”;
    // 文件命名
    String fileName = “图片.png”;
    // https文件下载链接
    String apiHttp = “https://gimg2.baidu.com/image_search/src=http%3A%2F%2Flmg.jj20.com%2Fup%2Fallimg%2F1114%2F040221103339%2F210402103339-8-1200.jpg&refer=http%3A%2F%2Flmg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1671956738&t=7369439221c4fff3114c8cbaa28b4330”;
    // 忽略对服务器端证书的校验
    SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(),
    NoopHostnameVerifier.INSTANCE);
    CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(scsf).build();
    HttpGet httpget = new HttpGet(apiHttp);
    HttpResponse response = client.execute(httpget);
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();
    // 对存储空间大小预定义
    int cache = 10 * 1024;
    // 文件输出路径
    FileOutputStream fileout = new FileOutputStream(savePath + “/” + fileName);
    byte[] buffer = new byte[cache];
    int ch = 0;
    while ((ch = is.read(buffer)) != -1) {
    fileout.write(buffer, 0, ch);
    }
    is.close();
    fileout.flush();
    fileout.close();

  • 相关阅读:
    智慧图书馆中的自助借还系统
    这年头谁还不会抓包,WireShark 抓包及常用协议分析送给你
    opencv-python图片转换、尺寸、传输
    ChatGLM2-6B模型尝鲜
    JavaSE——遍历Map集合
    点云中值滤波函数(附python open3d 实现)
    Python编程基础:实验6——函数的递归
    mysql(九)mysql主从复制
    在家呆了两天,今天聊下分布式压测
    关于git创建分支以及主分支相互合并操作记录
  • 原文地址:https://blog.csdn.net/m0_45081336/article/details/128040341