多线程下载(1)
一个文件实现多线程下载
- 例如下载一个视频的时候,文件很大却又想下载快些就可以使用多线程进行下载。多线程的方式把一个视频查分成多个视频进行下载,加快啦视频的下载速度
多个文件实现多线程
- 实现多个文件同时下载,通过爬虫的方式,或者其他渠道获得到的图片、视频路径(多个),如果成千上百个图片、视频还好你可以等一会,但是上万个,就算一个文件一秒,你也不相等把,如果是视频呢,所以就总结了一下,使用多线程的方式,同时下载多个文件。既加快啦速度页,节省啦时间。
下面实现一个文件的多线程下载 (完整版复制就可以使用)
-
创建一个StartThread 类继承 Runnable 实现多线程
package com.youzan.thread.onefilethread; // 类名的路径地址 改成你对应的类名地址 import java.io.File; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class StartThread implements Runnable{ private String urlLocation; private String filePath; private long start; private long end; public StartThread(String urlLocation, String filePath, long start, long end) { this.urlLocation = urlLocation; this.filePath = filePath; this.start = start; this.end = end; } @Override public void run() { try { System.out.println("进入多线程方法"); HttpURLConnection conn=getHttp(); conn.setRequestProperty("Range", "bytes=" + start + "-" + end); System.out.println("图片名称-->"+filePath); File file = new File(filePath); RandomAccessFile out = null; if(file !=null){ out = new RandomAccessFile(file,"rwd"); } out.seek(start); InputStream in = conn.getInputStream(); byte[] b = new byte[1024]; int len = 0; while ((len = in.read(b)) != -1){ out.write(b, 0, len); } in.close(); out.close(); } catch (Exception e) { System.out.println("download fail"); e.printStackTrace(); } } /** * 创建连接 * @return */ public HttpURLConnection getHttp(){ URL url = null; HttpURLConnection conn =null; try { if (urlLocation != null){ url = new URL(urlLocation); } conn= (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5000); conn.setRequestMethod("GET"); } catch (Exception e) { System.out.println("getHttp error"); } return conn; } }
-
创建CreateThread类实现创建线程以及对文件的分解
package com.youzan.thread.onefilethread; // 类名的路径地址 改成你对应的类名地址 import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CreateThread { /** * 实现多线程 方法 * @param urlLocation //网络图片地址 多个图片用逗号(,)隔开 使用 同时下载多个文件/// 一个图片使用 * @param filePath * @param poolLength * @throws IOException */ public void getFileWithThreadPool(String urlLocation,String filePath, int poolLength) throws IOException{ ExecutorService threadPool = Executors.newFixedThreadPool(poolLength); /** * 下载一个文件多线程下载(一个文件很大 可以由多个线程一起下载) */ long len = getContentLength(urlLocation); System.out.println("一个文件大小--》"+len+"--线程数--》"+poolLength); for(int i=0;i<poolLength;i++){ long start=i*len/poolLength; long end = (i+1)*len/poolLength-1; System.out.println("start-->"+start+"--end-->"+end); if(i==poolLength-1){ end =len; System.out.println("len 赋值"); } //创建多个线程 StartThread download=new StartThread(urlLocation, filePath+"140G61501440-L.jpg", start,end); threadPool.execute(download); } //让线程执行完自动关闭 threadPool.shutdown(); System.err.println("分配线程 任务结束"); } /** * 获取文件的大小 (单位 byte) * @param urlLocation * @return * @throws IOException */ private static long getContentLength(String urlLocation) throws IOException{ URL url = null; if (urlLocation != null) { url = new URL(urlLocation); } HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5000); conn.setRequestMethod("GET"); long len = conn.getContentLength(); return len; } }
-
实现类main
public class text { public static void main(String[] args) { try { //一个文件 多线程下载 String url="http://img.tupianzj.com/uploads/allimg/140716/3-140G61501440-L.jpg"; CreateThread createThread=new CreateThread(); // 网络文件url --- 存放路径 --- 创建的线程数 createThread.getFileWithThreadPool(url,"D:/临时/threadimg/",3); } catch (Exception e) { e.printStackTrace(); } } }
【一个文件分成多个线程下载就完成啦】
有什么疑惑,或者问题,在下面留言。或者扫码加好友一起探索更深的奥秘
【免费】 分布式开发教程,redis 缓存,以及 session 共享 等开发视频教程请扫码获取,全部免费
(微信扫码)