java訪問https鏈接下載圖片
一、通過maven引入https工具包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
二、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 {
// 文件下載存儲(chǔ)路徑
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";
// 忽略對(duì)服務(wù)器端證書的校驗(yàn)
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();
// 對(duì)存儲(chǔ)空間大小預(yù)定義
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();
}
}
三、https鏈接下載文件工具類講解
// 文件下載存儲(chǔ)路徑
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”;
// 忽略對(duì)服務(wù)器端證書的校驗(yàn)
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();
// 對(duì)存儲(chǔ)空間大小預(yù)定義
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();文章來源地址http://www.zghlxwxcb.cn/news/detail-404406.html
文章來源:http://www.zghlxwxcb.cn/news/detail-404406.html
到了這里,關(guān)于java訪問https鏈接下載圖片的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!