总就三个东西,如下图:第一个txt文件就是有图片的url链接地址
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-11572548.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-11593467.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-11631922.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-12203460.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-12240136.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-10092819.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-10519162.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-10977780.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-11311195.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-11954484.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-12072057.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-12072057.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-4542338.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-6323680.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-6873171.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-7409835.jpeg
第二个就是 properties文件,指定线程数量和本地的下载文件夹绝对路径:
注意,thread-num=6,6的后面不能有空格。正确的路径写法:target-dir = e:/test/1111/ 错误的:target-dir = e:/test/1111 or target-dir = e:test/1111
第三个就是downloader类:
最终的bug改好了,原因是把新建文件夹
一级文件夹:dir.mkdir() 多级文件夹: dir.mkdirs()
,错误写成了新建文件:dir.createNewFile()
完整代码如下:
package qiu.downloader;
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 Downloader {
private int threadNum = 0;
public static void main(String[] args) throws IOException {
Downloader downloader = new Downloader();
// String urlStr = new String("https://img2.baidu.com/it/u=419672121,2062370122&fm=253&fmt=auto&app=138&f=PNG?w=500&h=378");
// downloader.urlDown(urlStr,"e:/test/1109/a.jpg");
String pictureTxt = new String("D:\\ideaWorkspace\\chapter11month\\io\\src\\picture.txt");
String configDir = new String("D:\\ideaWorkspace\\chapter11month\\io\\src\\config.properties");
downloader.multiDownload(pictureTxt,configDir);
}
/**
* 根据完整下载资源主要是图片到本地
* @param urlStr 网址
* @param dir 目录路径字符
*/
public void urlDown(String urlStr,String dir){
InputStream inputStream = null;
OutputStream outputStream = null;
File file = new File(dir);//创建文件,dir=绝对路径+图片名称后缀
try {
URL url = new URL(urlStr);
URLConnection urlConnection = url.openConnection();
inputStream = urlConnection.getInputStream();
outputStream = new FileOutputStream(file);
byte[] bytes = new byte[1024];
int n = 0;
String str = null;
while((n = inputStream.read(bytes)) != -1){
outputStream.write(bytes,0,n);
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
/**
*
* @param pictureTxt picture.txt绝对地址,"D:\\ideaWorkspace\\chapter11month\\io\\src\\picture.txt"
* @param configDir config.properties的绝对路径,D:\ideaWorkspace\chapter11month\io\src\config.properties
*/
public void multiDownload(String pictureTxt,String configDir) throws IOException {
//读取pictureTxt的每一行内容,也就是每一行一个下载地址
FileReader fileReader = new FileReader(pictureTxt);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String str = null;
List<String> list = new ArrayList<>();
//网络上的图片下载链接地址
while ((str = bufferedReader.readLine()) != null){
list.add(str);
}
//读取config文件,即threadNum和targetDir
FileReader configReader = new FileReader(configDir);
Properties properties = new Properties();
properties.load(configReader);
String threadNum = properties.getProperty("thread-num");
this.threadNum = Integer.parseInt(threadNum);
String targetDir = properties.getProperty("target-dir");
//先创建一个目标目录对象,保存在本地的目录
File dir = new File(targetDir);
//dir == null
if (!dir.exists()){
System.out.println("目录不存在,现在开始创建文件夹");
// boolean isCreateFile = dir.createNewFile();//这是新建文件
boolean mkdir = dir.mkdir();
if (mkdir) {
System.out.println("目录创建成功,下载图片中");
}else {
System.out.println("目录创建失败");
}
}else {
System.out.println("目录存在,无需创建,下载图片中");
}
//开始创建多线程进行批量下载
ExecutorService executorService = Executors.newFixedThreadPool(this.threadNum);
Downloader downloader = this;
for (String picUrl : list) {
System.out.println("picUrl:"+picUrl);
int index = picUrl.lastIndexOf("/");
String targetJpg = picUrl.substring(index+1);
String lastDir = new String(targetDir+"/"+targetJpg);//注意一定要加上 /
executorService.execute(new Runnable() {
@Override
public void run() {
downloader.urlDown(picUrl,lastDir);
}
});
}
//关闭线程池和字节输入流
executorService.shutdown();
bufferedReader.close();
}
}