通過圖片的鏈接(如:https://upload-images.jianshu.io/upload_images/11722017-e806b092026a0502.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/628/format/webp)
將圖片下載到本地幅垮,并且返回保存的路徑遮咖,然后將路徑保存到數(shù)據(jù)庫中,由于數(shù)據(jù)量較多亭病,可以通過使用多線程的方式縮短處理的時間
線程類
/**
* 使用多線程處理批量圖片的保存操作
* @version 1.0.0
*/
public class RunnableSavePicture implements Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(RunnableSavePicture.class);
// 空List陶夜,用于存放已經(jīng)處理好圖片的TrademarkInfoDTO對象
private List<TrademarkInfoDTO> emptySaveDataList;
// 傳遞過來的包含圖片鏈接的對象
private TrademarkInfoDTO trademarkInfoDTO;
// 保存路徑
private String filePath;
public RunnableSavePicture(TrademarkInfoDTO trademarkInfoDTO, List<TrademarkInfoDTO> emptySaveDataList, String filePath) {
this.trademarkInfoDTO = trademarkInfoDTO;
this.emptySaveDataList = emptySaveDataList;
this.filePath = filePath;
}
@Override
public void run() {
try {
String oldUrl = trademarkInfoDTO.getTrademarkImgPath();
if (!StringUtils.isBlank(oldUrl)) {
byte[] imageByte = null;
// 用于請求圖片不成功時翰意,再次請求
int count = 0;
while (null == imageByte || imageByte.length < 1024) {
// 通過鏈接請求圖片返回byte[]用于保存
imageByte = HttpUtil.getPicutre(oldUrl);
// 3次失敗則放棄
if (count > 3) break;
count++;
}
// 使用UUID生成圖片文件名
String fileName = UUID.randomUUID().toString().replaceAll("-", "");
// 將圖片保存到本地
saveFile(filePath, imageByte, fileName, "jpg");
// 設(shè)置路徑
trademarkInfoDTO.setTrademarkImgPath("/icon/" + fileName + ".jpg");
// 添加到空List中(用于保存到數(shù)據(jù)庫)
emptySaveDataList.add(trademarkInfoDTO);
}
} catch (Exception e) {
e.printStackTrace();
LOGGER.debug("==【線程內(nèi)保存圖片操作】==執(zhí)行異常约郁,注冊號:" + trademarkInfoDTO.getTrademarkNum());
LOGGER.debug("==【線程內(nèi)保存圖片操作】==執(zhí)行異常" + e);
}
}
/*
* @描述:保存圖片
* @param:[basePath-存儲路徑, content-圖片byte, fileName-圖片名稱, fileType-圖片類型]
* @return:void
*/
private static void saveFile(String basePath, byte[] content, String fileName, String fileType) throws Exception {
File savePath = new File(basePath);
if (!savePath.exists()) {
savePath.mkdir();
}
File file = new File(savePath.getAbsolutePath() + "/" + fileName + "." + fileType);
FileOutputStream fos = new FileOutputStream(file);
fos.write(content);
fos.close();
}
}
使用方法
public static void main(String[] args) {
// 從數(shù)據(jù)庫中查詢出圖片鏈接
List<TrademarkInfoDTO> canSaveDataList = trademarkTemporaryMapper.getCanSaveDataList(adminUid);
// 當(dāng)前為空List战惊,在線程內(nèi)處理好圖片后流昏,會將傳遞過去的TrademarkInfoDTO對象放進(jìn)該List中
List<TrademarkInfoDTO> emptySaveDataList = new ArrayList<>();
long startTime = System.currentTimeMillis();
String filePath = "E:\\icon\\";
int availProcessors = Runtime.getRuntime().availableProcessors();
LOGGER.debug("==【多線程處理圖片】==使用線程數(shù):" + (availProcessors * 2));
ExecutorService executorService = Executors.newFixedThreadPool(availProcessors * 2);
for (TrademarkInfoDTO trademarkInfoDTO : canSaveDataList) {
executorService.execute(new RunnableSavePicture(trademarkInfoDTO, emptySaveDataList, filePath));
}
executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
LOGGER.debug("==【多線程處理圖片】==圖片保存執(zhí)行完畢,共保存圖片:" + emptySaveDataList.size() +
"張吞获,用時:" + (System.currentTimeMillis() - startTime));
// 執(zhí)行批量插入操作
int count = trademarkTemporaryMapper.saveAllCrawlTrademark(emptySaveDataList);
LOGGER.debug("==【保存數(shù)據(jù)操作執(zhí)行完畢】==共保存:" + count);
}
用到的 HttpUtil 工具類
public class HttpUtil {
/*
* @描述:通過圖片鏈接下載圖片并返回byte[]
* @param:[url]
* @return:byte[]
*/
public static byte[] getPicutre(String url) throws Exception {
byte[] data;
try {
Request request = new Request.Builder().url(url).build();
com.squareup.okhttp.Response response = CLIENT.newCall(request).execute();
data = response.body().bytes();
} catch (Exception e) {
return null;
}
return data;
}
}
用到okhttp况凉,pom.xml需要導(dǎo)入下面兩個jar包
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp/okhttp -->
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.squareup.okio/okio -->
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>1.13.0</version>
</dependency>