多线程下载(2)

一个文件实现多线程下载
  • 例如下载一个视频的时候,文件很大却又想下载快些就可以使用多线程进行下载。多线程的方式把一个视频查分成多个视频进行下载,加快啦视频的下载速度
多个文件实现多线程
  • 实现多个文件同时下载,通过爬虫的方式,或者其他渠道获得到的图片、视频路径(多个),如果成千上百个图片、视频还好你可以等一会,但是上万个,就算一个文件一秒,你也不相等把,如果是视频呢,所以就总结了一下,使用多线程的方式,同时下载多个文件。既加快啦速度页,节省啦时间。

下面实现多个文件的多线程下载 (完整版复制就可是使用)

  1. 创建一个StartThread 类继承 Runnable 实现多线程
package com.youzan.thread.morethread;  //换成你对应的包路径
  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();
  			
  		}
  		
  	}
  		
  		
  		 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;
  	     }
  		 
  }
  1. 创建MoreFileCreateThread类实现创建线程以及对多个文件的分配工作
package com.youzan.thread.morethread;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MoreFileCreateThread {
	/**
	 * 实现多线程 方法
	 * @param urlLocation //网络图片地址    多个图片用逗号(,)隔开 使用 同时下载多个文件///  一个图片使用 
	 * @param filePath
	 * @param poolLength
	 * @throws IOException
	 */
	public void getFileWithThreadPool(String urlLocation,String filePath, int poolLength) throws IOException{
        
		ExecutorService threadPool = Executors.newFixedThreadPool(poolLength);
        
        
        /**
         * 同时下载多个文件
         */
        String[] arrayStrings=urlLocation.split(",");
        poolLength=arrayStrings.length;
        
        System.out.println("线程数"+poolLength);
        
        //也可以写一个固定的线程数  一个线程里面执行一份部分下载
        
		for(int a=0;a<arrayStrings.length;a++){
			System.out.println(a+"长度"+arrayStrings[a]);
			long len = getContentLength(arrayStrings[a]);
			
			StartThread download=new StartThread(arrayStrings[a], filePath+a+".jpg",0,len);
            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;
    }
}
  1. 实现类main
package com.youzan.thread.morethread;
public class text {
	public static void main(String[] args) {
		try {
			
			//多个文件多线程下载  用逗号(,) 隔开
			String url="http://img.tupianzj.com/uploads/allimg/140716/3-140G61501440-L.jpg, http://img.tupianzj.com/uploads/allimg/140418/3-14041Q43F40-L.jpg, http://img.tupianzj.com/uploads/allimg/140319/4-1403191055450-L.jpg, http://img.tupianzj.com/uploads/allimg/131120/3-1311200954320-L.jpg, http://img.tupianzj.com/uploads/allimg/130802/4-130P21036010-L.jpg, http://img.tupianzj.com/uploads/allimg/170107/9-1F10H004310-L.jpg, http://img.tupianzj.com/uploads/allimg/180523/9-1P5231000080-L.jpg, http://img.tupianzj.com/uploads/allimg/170309/9-1F3092145190-L.jpg, http://img.tupianzj.com/uploads/allimg/161107/9-16110H205210-L.jpg, http://img.tupianzj.com/uploads/allimg/170107/9-1F10H004310-L.jpg, http://img.tupianzj.com/uploads/allimg/140604/3-1406041414480-L.jpg, http://img.tupianzj.com/uploads/allimg/171017/9-1G01G15T00-L.jpg, http://img.tupianzj.com/uploads/allimg/170805/9-1FP51IA80-L.jpg, http://img.tupianzj.com/uploads/allimg/180331/9-1P3311131060-L.jpg, http://img.tupianzj.com/uploads/allimg/140102/3-1401021142510-L.jpg, http://img.tupianzj.com/uploads/allimg/161121/9-1611212039430-L.jpg, http://img.tupianzj.com/uploads/allimg/150317/7-15031G511460-L.jpg, http://img.tupianzj.com/uploads/allimg/180202/9-1P2021AS60-L.jpg, http://img.tupianzj.com/uploads/allimg/180202/9-1P2021A5070-L.jpg";
			
			MoreFileCreateThread createThread=new MoreFileCreateThread();
			// 网络文件url --- 存放路径 --- 创建的线程数
			createThread.getFileWithThreadPool(url,"D:/临时/threadimg/",3);
	
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
【多个文件多个线程下载就完成啦】

有什么疑惑,或者问题,在下面留言。