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很小,这样的图片记录下来