1. 程式人生 > >java多執行緒根據檔案裡的URL下載圖片

java多執行緒根據檔案裡的URL下載圖片

 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue; import com.springboot.ImgInfo; import com.springboot.RunnableImg; public class DownloadJpg { public static void main(String args[]) throws Exception{ LinkedBlockingQueue <ImgInfo> queue = new LinkedBlockingQueue<ImgInfo>(); download(queue); }
public static void download(LinkedBlockingQueue <ImgInfo> queue) throws Exception { String dirPath = "D:\\downloadopenset"; File f = new File(dirPath); File[] fs = f.listFiles(); int count=0; for(File file:fs) { BufferedReader reader
= new BufferedReader(new FileReader(file)); int line=0; String str; while((str=reader.readLine())!=null) { line++; if(line==1)continue; String[] info = str.split(" "); String urlString = info[0];//str.split(" ")[0]; String md5String = info[2]; String dirName=file.getName(); count++; ImgInfo imageInfo = new ImgInfo(urlString,md5String,dirName,count); queue.put(imageInfo); } reader.close(); } RunnableImg r1 = new RunnableImg(queue,"r1"); RunnableImg r2 = new RunnableImg(queue,"r2"); RunnableImg r3 = new RunnableImg(queue,"r3"); RunnableImg r4 = new RunnableImg(queue,"r4"); r1.start(); r2.start(); r3.start(); r4.start(); } }

 

繼承Thread類

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;

public class RunnableImg extends Thread{    
    private Thread t;
    private String threadName;    
    LinkedBlockingQueue <ImgInfo> queue;
    RunnableImg(LinkedBlockingQueue <ImgInfo> queue,String name) {
        this.queue=queue;        
        threadName=name;
        System.out.println("Creating "+threadName);
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        Date d = new Date();        
        System.out.println("Running "+threadName);
        while(true) {
            try {            
                ImgInfo currentimg = queue.take();
                System.out.println(threadName+" "+currentimg.urlString+" is downloading");
                
                URL url = new URL(currentimg.urlString);
                HttpURLConnection  con = (HttpURLConnection) url.openConnection();
                InputStream is = con.getInputStream();                
                int len;                    
                String saveImgPath = "G:\\"+currentimg.dirName;
                File sf = new File(saveImgPath);
                if (!sf.exists()) {
                    sf.mkdirs();
                }
                String loadPath= sf.getPath()+"\\"+currentimg.md5String+".jpg";
                
                File check = new File(loadPath);
                if (!check.exists()) {                    
                    OutputStream os = new FileOutputStream(loadPath);
                    while((len = is.read())!=-1) {                    
                        os.write(len);
                    }
                    os.flush();
                    os.close();
                    System.out.println(d.toString()+" "+threadName+" "+currentimg.urlString+" downloaded Success "+currentimg.count);
                }                
                is.close();                
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                System.out.println("Fail "+threadName);
                e.printStackTrace();
            } catch (IOException e) {
                System.out.println("Fail "+threadName);
                e.printStackTrace();
            }
        }        
    }
}

 

queue傳遞的物件

public class ImgInfo {
    String urlString;
    String md5String;
    String dirName;
    int count;
    public String getUrlString() {
        return urlString;
    }
    public void setUrlString(String urlString) {
        this.urlString = urlString;
    }
    public String getMd5String() {
        return md5String;
    }
    public void setMd5String(String md5String) {
        this.md5String = md5String;
    }
    public String getDirName() {
        return dirName;
    }
    public void setDirName(String dirName) {
        this.dirName = dirName;
    }
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }

    public ImgInfo(String urlString,String md5String,String dirName,int count) {
        this.urlString = urlString;
        this.md5String = md5String;
        this.dirName = dirName;
        this.count = count;
    }
}

 想增加:1、日誌記錄 2、圖片下載中斷後,位元組數不足,判斷圖片沒下載完重新下載或者接著下載 3、有圖片本身就失效了,圖片大概3k很小,這樣的圖片記錄下來