本篇內(nèi)容主要講解“java線程池怎么實(shí)現(xiàn)批量下載文件”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“java線程池怎么實(shí)現(xiàn)批量下載文件”吧!
成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的三都網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
本文實(shí)例為大家分享了java線程池實(shí)現(xiàn)批量下載文件的具體代碼,供大家參考,具體內(nèi)容如下
1 創(chuàng)建線程池
package com.cheng.webb.thread;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.ThreadFactory;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicInteger;public class ThreadUtil { /** * 創(chuàng)建批量下載線程池 * * @param threadSize 下載線程數(shù) * @return ExecutorService */ public static ExecutorService buildDownloadBatchThreadPool(int threadSize) { int keepAlive = 0; String prefix = "download-batch"; ThreadFactory factory = ThreadUtil.buildThreadFactory(prefix); return new ThreadPoolExecutor(threadSize, threadSize, keepAlive, TimeUnit.SECONDS, new ArrayBlockingQueue<>(threadSize), factory); } /** * 創(chuàng)建自定義線程工廠 * * @param prefix 名稱前綴 * @return ThreadFactory */ public static ThreadFactory buildThreadFactory(String prefix) { return new CustomThreadFactory(prefix); } /** * 自定義線程工廠 */ public static class CustomThreadFactory implements ThreadFactory { private String threadNamePrefix; private AtomicInteger counter = new AtomicInteger(1); /** * 自定義線程工廠 * * @param threadNamePrefix 工廠名稱前綴 */ CustomThreadFactory(String threadNamePrefix) { this.threadNamePrefix = threadNamePrefix; } @Override public Thread newThread(Runnable r) { String threadName = threadNamePrefix + "-t" + counter.getAndIncrement(); return new Thread(r, threadName); } }}
2 批量下載文件
package com.cheng.webb.thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.concurrent.*;/** * 文件下載類 * * @author shucheng * @creation 2019年1月30日下午4:41:32 */public class DownloadUtil { private static Logger logger = LoggerFactory.getLogger(DownloadUtil.class); /** * 下載線程數(shù) */ private static final int DOWNLOAD_THREAD_NUM = 14; /** * 下載線程池 */ private static ExecutorService downloadExecutorService = ThreadUtil .buildDownloadBatchThreadPool(DOWNLOAD_THREAD_NUM); /** * 文件下載 * * @param fileUrl * 文件url,如:https://img3.doubanio.com//view//photo//s_ratio_poster//public//p2369390663.webp
* @param path * 存放路徑,如: /opt/img/douban/my.webp */ public static void download(String fileUrl, String path) { // 判斷存儲(chǔ)文件夾是否已經(jīng)存在或者創(chuàng)建成功 if (!createFolderIfNotExists(path)) { logger.error("We can't create folder:{}", getFolder(path)); return; } InputStream in = null; FileOutputStream out = null; try { URL url = new URL(fileUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); // 2s conn.setConnectTimeout(10000); in = conn.getInputStream(); out = new FileOutputStream(path); int len; byte[] arr = new byte[1024 * 1000]; while (-1 != (len = in.read(arr))) { out.write(arr, 0, len); } out.flush(); conn.disconnect(); } catch (Exception e) { logger.error("Fail to download: {} by {}", fileUrl, e.getMessage()); } finally { try { if (null != out) { out.close(); } if (null != in) { in.close(); } } catch (Exception e) { // do nothing } } } /** * 創(chuàng)建文件夾,如果文件夾已經(jīng)存在或者創(chuàng)建成功返回true * * @param path * 路徑 * @return boolean */ private static boolean createFolderIfNotExists(String path) { String folderName = getFolder(path); if (folderName.equals(path)) { return true; } File folder = new File(getFolder(path)); if (!folder.exists()) { synchronized (DownloadUtil.class) { if (!folder.exists()) { return folder.mkdirs(); } } } return true; } /** * 獲取文件夾 * * @param path * 文件路徑 * @return String */ private static String getFolder(String path) { int index = path.lastIndexOf("/"); return -1 != index ? path.substring(0, index) : path; } /** * 下載資源 *
* issue: 線程池創(chuàng)建過(guò)多 *
* 最大批量下載為5,請(qǐng)知悉 * * @param resourceMap * 資源map, key為資源下載url,value為資源存儲(chǔ)位置 */ public static void batch(Map
3 測(cè)試批量下載文件
package com.cheng.webb.thread;import java.util.HashMap;import java.util.Map;import org.junit.Test;import com.alibaba.fastjson.JSON;public class DownLoadTest { String json = "{\r\n" + " \"http://www.xxx.com/111/123.mp4\":\"myFile/111/123.mp4\",\r\n" + " \"http://www.xxx.com/111/124.mp4\":\"myFile/111/124.mp4\",\r\n" + " \"http://www.xxx.com/111/125.mp4\":\"myFile/111/125.mp4\"\r\n" + "}"; @SuppressWarnings("unchecked") @Test public void test() { Map
到此,相信大家對(duì)“java線程池怎么實(shí)現(xiàn)批量下載文件”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!