import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// 定义一个多线程下载器
// 需要读入配置信息和本地保存的网址,将内容下载在本地
public class DownLoad {
private Integer threadNum=10;//线程池数量
/**
*传入网络地址,获取网络资源下载到本地
* @param source 网络地址
* @param targetDir 本地的目录
*/
public void downloader(String source,String targetDir){
InputStream is = null;
FileOutputStream fos = null;
try {
// https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-7409835.jpeg
/*想要获取输入网址的最后一个斜杠作为保存的图片的名字*/
String fileName = source.substring(source.lastIndexOf("/") + 1);//拿到了pexels-photo-7409835.jpeg
File targetFile = new File(targetDir + "/" + fileName);//拿到本地该文件
if (!targetFile.exists()){ //如果该文件不存在,就需要创建
targetFile.createNewFile();
}
URL url = new URL(source);
URLConnection connection = url.openConnection(); //打开连接
is = connection.getInputStream();
fos = new FileOutputStream(targetFile);
byte[] bs = new byte[1024]; //每次读入的内存大小
int ch=0; //长度
while ((ch=is.read(bs))!=-1){
fos.write(bs,0,ch); // 写数据在本地
}
System.out.println("图片下载完毕="+"目标文件地址"+targetFile.getPath()+"文件大小:"+ Math.floor(targetFile.length()/1024)+"kb");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (fos!=null){
fos.close();
}
if (is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*获取properties文件的内容
* @param propDir 保存properties文件的上一级目录
*/
public void start(String propDir){
/*获取properties文件*/
File file = new File(propDir+"\\config.properties");
Properties properties=null;
Reader reader=null;
try {
// 加载
properties = new Properties();
reader = new FileReader(file);
properties.load(reader);
// 读取内容
String threadNum = properties.getProperty("thread-num");
String targetDir = properties.getProperty("tagert-dir");
System.out.println(threadNum);
System.out.println(targetDir);
//将从properties文件拿到的线程池数量放到当前对象
this.threadNum=Integer.parseInt(threadNum);
//从指定文件读取地址,下载网络资源到本地
this.multiDownLoadFromFile(targetDir,propDir+"\\download.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (reader!=null){
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*从指定文件读取网络地址,下载网络资源到本地
* @param targetDir 保存在本地文件的目录
* @param downloadText 下载文件的目录
*/
public void multiDownLoadFromFile(String targetDir,String downloadText){
BufferedReader reader=null;
File dir=null;
ExecutorService threadPool=null;
try {
dir = new File(targetDir);
if (!dir.exists()){
dir.mkdirs();
System.out.println("目录已创建:"+dir.getName()+dir.getPath());
}
reader=new BufferedReader(new FileReader(downloadText));//读取本地文件
String line=null;
List<String> fileName=new ArrayList<>();
while((line=reader.readLine())!=null){
fileName.add(line);//拿到每个地址保存到集合里
System.out.println(line);
}
threadPool = Executors.newFixedThreadPool(this.threadNum);//线程池里有10个线程
DownLoad that=this;
for (String f:fileName){
threadPool.execute(new Runnable() {//每个线程
@Override
public void run() {
that.downloader(f,targetDir);
}
});
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (threadPool!=null){
threadPool.shutdown();
}
if (reader!=null){
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
DownLoad downLoad = new DownLoad();
downLoad.start("E:\\Workspace4\\spring\\work_project\\src\\main\\java\\com\\dxw\\test\\io\\download");
// downLoad.downloader("https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-12072057.jpeg","E:/");
}
}
thread-num=10 tagert-dir=E:/
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-7409835.jpeg