一个Java实现的工具,能下载Github issue里包含的图片到本地

package youdao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class GithubIssueImageDownloader {

	private static final String PREFIX = "C:\\Users\\i042416\\Pictures\\";
	private static final String ISSUEURL = "C:\\Users\\i042416\\git\\JavaTwoPlusTwoEquals5\\src\\youdao\\privateIssue.txt";
	
	private static void createFolder(String title){
		DownloadTask.FOLDER = PREFIX + title;
		File file = new File( DownloadTask.FOLDER);
		file.mkdir();
	}
	
	private static String getUrl(String line){
		int start = line.indexOf("(");
		int last = line.indexOf(")");
		return line.substring(++start, last);
	}
	private static List<DownloadTask> getPicUrlList(){
		int index = 0;
		List<DownloadTask> resultPic = new ArrayList<DownloadTask>();
		BufferedReader br = null;
		try {	
			 br = new BufferedReader(new InputStreamReader(new FileInputStream(ISSUEURL ), "utf-8")); 
			 String line = null;
			 while ((line = br.readLine()) != null) {
				line = getUrl(line);
				DownloadTask task = new DownloadTask( line, index++);
				resultPic.add(task);
			 }
			 br.close();
		}
		catch (FileNotFoundException e) {
				e.printStackTrace();
		} catch (IOException e1) {
				e1.printStackTrace();
		}
		return resultPic;
	}
	
	private static void start(List<DownloadTask> task){
		if( task.isEmpty()){
			System.out.println("No picture to download!");
			return;
		}
		System.out.println("Total pic to be downloaded: " + task.size());
		ExecutorService executor = Executors.newFixedThreadPool(10);
		
		for( int i = 0; i < task.size(); i++){
			PictureDownloader cc = new PictureDownloader(task.get(i));
			executor.execute(cc);			
		}
		
		executor.shutdown();
		while (!executor.isTerminated()) {
        }
		
		System.out.println("download finished");
	}
	
	public static void main(String[] args) {
		createFolder("Test");
		List<DownloadTask> task = getPicUrlList();
		start(task);
	}
}

展开阅读全文
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值