Https图片链接下载问题

1. 获取方法

入参是一个Url, 和一个随机的名称. 返回值是MultipartFile, 这里因为我这里需要调接口传到服务器, 这里也可以直接通过inputStream进行操作. 按需修改

java 复制代码
 /**
     * 通过Url获取文件
     *
     * @param url
     * @param fileName 随机产生一个文件名, 可以是uuid等
     * @return
     * @throws Exception
     */
    public static MultipartFile doGetForHttpsUrl(String url, String fileName) throws Exception {
        CloseableHttpClient httpClient = getPassSSLCloseableHttpClient();
        InputStream inputStream = null;
        try {
            HttpGet httpGet = new HttpGet(url);
            // 第二个参数是user-agent 可以在网页的检查中看到
            httpGet.setHeader("User-Agent", getOneUserAgent());
            CloseableHttpResponse response = httpClient.execute(httpGet);
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    inputStream = entity.getContent();
                    MultipartFile multipartFile = toMultipartFile(inputStream, fileName + ".png");
                    response.close();
                    return multipartFile;
                }
            }
        } finally {
            if (httpClient != null) {
                httpClient.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
        return null;
    }

    /**
     * 将InputStream转换成MultipartFile
     * 这里是因为需求需要,所以转为了一下具体的根据实际情况返回需要的值
     * @param inputStream 输入流
     * @param fileName    文件名
     * @return
     */
    public static MultipartFile toMultipartFile(InputStream inputStream, String fileName) throws IOException {
        File tempFile = File.createTempFile("multipart-file", ".tmp");
        FileUtils.copyInputStreamToFile(inputStream, tempFile);
        byte[] bytes = IOUtils.toByteArray(new FileInputStream(tempFile));
        return new MockMultipartFile(tempFile.getName(), fileName, "image/jpeg", bytes);
    }
2. 信任所有的整数解决证书问题
java 复制代码
    /**
     * 信任所有的证书
     *
     * @return
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     */
    private static CloseableHttpClient getPassSSLCloseableHttpClient() throws NoSuchAlgorithmException, KeyManagementException {
        // 创建信任所有证书的 TrustManager
        TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
                    public void checkClientTrusted(X509Certificate[] certs, String authType) {
                    }
                    public void checkServerTrusted(X509Certificate[] certs, String authType) {
                    }
                }
        };
        // 获取 SSL 上下文
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());

        // 将默认的 SSLSocketFactory 和 HostnameVerifier 替换为信任所有证书的版本
        return HttpClients.custom().setSSLContext(sc)
                .setSSLHostnameVerifier(new NoopHostnameVerifier())
                .build();
    }

获取userAgent的方法在 Https接口调用问题的最后一个title有需要的话可以看一下

相关推荐
Kuo-Teng1 分钟前
LeetCode 73: Set Matrix Zeroes
java·算法·leetcode·职场和发展
王元_SmallA4 分钟前
服务器公网IP、私网IP、弹性IP是什么?区别与应
java·后端
葵续浅笑23 分钟前
LeetCode - 杨辉三角 / 二叉树的最大深度
java·数据结构·算法·leetcode
2501_9159214328 分钟前
iOS 抓不到包怎么办?工程化排查与替代抓包方案(抓包/HTTPS/Charles代理/tcpdump)
android·ios·小程序·https·uni-app·iphone·tcpdump
装不满的克莱因瓶33 分钟前
【Java架构师】各个微服务之间有哪些调用方式?
java·开发语言·微服务·架构·dubbo·restful·springcloud
N 年 后1 小时前
cursor和传统idea的区别是什么?
java·人工智能·intellij-idea
CodeLongBear1 小时前
从Java后端到Python大模型:我的学习转型与规划
java·python·学习
Miraitowa_cheems1 小时前
LeetCode算法日记 - Day 94: 最长的斐波那契子序列的长度
java·数据结构·算法·leetcode·深度优先·动态规划
Jerry2505091 小时前
怎么才能实现网站HTTPS访问?
网络协议·http·网络安全·https·ssl
Zz_waiting.1 小时前
统一服务入口-Gateway
java·开发语言·gateway