题目
现有如下网络图片地址:
https://imgsa.baidu.com/forum/w%3D580/sign=43e292947c1ed21b79c92eed9d6fddae/6bfab2fb43166d228b3c16f2472309f79052d20a.jpg
https://imgsa.baidu.com/forum/w%3D580/sign=ead34ea6f636afc30e0c3f6d8318eb85/eb38b6003af33a8700cc037cc75c10385343b530.jpg
...
https://imgsa.baidu.com/forum/w%3D580/sign=4da50e8db7fd5266a72b3c1c9b199799/a623720e0cf3d7cab7a53d10f31fbe096b63a902.jpg
https://imgsa.baidu.com/forum/w%3D580/sign=b00697ba09fa513d51aa6cd60d6c554c/b25594eef01f3a290c7cd7a89825bc315c607c24.jpg
https://imgsa.baidu.com/forum/w%3D580/sign=2329896332fa828bd1239debcd1f41cd/8d1d8701a18b87d6b868a679060828381f30fdab.jpg
使用多线程下载图片文件(将网络上的图片拷贝到本地)
要求:
-
.利用线程池实现
-
同一时刻最多运行 10 个任务
-
主线程等待所有文件下载结束,打印信息【下载完成】
我的作答
img.txt
https://imgsa.baidu.com/forum/w%3D580/sign=43e292947c1ed21b79c92eed9d6fddae/6bfab2fb43166d228b3c16f2472309f79052d20a.jpg
https://imgsa.baidu.com/forum/w%3D580/sign=ead34ea6f636afc30e0c3f6d8318eb85/eb38b6003af33a8700cc037cc75c10385343b530.jpg
...
https://imgsa.baidu.com/forum/w%3D580/sign=4da50e8db7fd5266a72b3c1c9b199799/a623720e0cf3d7cab7a53d10f31fbe096b63a902.jpg
https://imgsa.baidu.com/forum/w%3D580/sign=b00697ba09fa513d51aa6cd60d6c554c/b25594eef01f3a290c7cd7a89825bc315c607c24.jpg
https://imgsa.baidu.com/forum/w%3D580/sign=2329896332fa828bd1239debcd1f41cd/8d1d8701a18b87d6b868a679060828381f30fdab.jpg
ImgFiles.java
public class ImgFiles {
// 【图片地址库】用于装箱
private ArrayList<String> list;
public ImgFiles() {
}
public ImgFiles(ArrayList<String> list) {
this.list = list;
}
public ArrayList<String> getList() {
return list;
}
public void setList(ArrayList<String> list) {
this.list = list;
}
}
ThreadRunnable.java
public class ThreadRunnable implements Runnable {
private final ImgFiles img;
public ThreadRunnable(ImgFiles img) {
this.img = img;
}
@Override
public void run() {
// 获取【图片地址库】
ArrayList<String> list = img.getList();
// while (true){
// 随时更新【图片地址库】的地址:在【图片地址副本】中进行操作,当获取到地址后删除本地址,并将副本覆盖至【图片地址库】。
String imgURL = list.get(0);
list.remove(0);
img.setList(list);
// 当前图片地址为:
// imgURL = "https://imgsa.baidu.com/forum/w%3D580/sign=43e292947c1ed21b79c92eed9d6fddae/6bfab2fb43166d228b3c16f2472309f79052d20a.jpg"
// 本地存储【图片名字】应该为 imgURL.split("/")后,应为imgFileName = "6bfab2fb43166d228b3c16f2472309f79052d20a.jpg"
String[] imgURLSplitArrays = imgURL.split("/");
String imgFileName = imgURLSplitArrays[imgURLSplitArrays.length - 1]; // 取出URL中的img名字
try {
// 进行包装前-------本地存储【图片路径】应该为: imgFilePath = "存放路径" + "\\" + "imgFileURL"
File imgDeposit = new File("C:\\Users\\Ensio\\Desktop\\HeiMa\\题库(2)\\题库\\robin\\目标数据" + "\\" + imgFileName); // 最终图片存放地址
/*
URL(String spec) 从 String表示形成一个 URL对象。
URL url = null;
URL url = new URL(imgURL);
openStream() 获取URL打开与此 URL ,并返回一个 InputStream ,以便从该连接读取。
URLConnection conn = new URL(imgURL).openConnection();
*/
// 获取 URLInputStream字节流
InputStream in = new URL(imgURL).openConnection().getInputStream();
// 将获取的URL字节流 写入 本地
OutputStream ot = new FileOutputStream(imgDeposit);
byte[] bytes = new byte[1024];
int len;
while ((len = in.read(bytes)) != -1){
ot.write(bytes, 0, len);
}
// 提示语句:子线程名 : 图片名 写入成功!
if (imgDeposit.exists()){
System.out.println(Thread.currentThread().getName() + " : " + imgDeposit.getName() + "写入成功!");
}
// 释放资源
in.close();
ot.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//}
}
}
MainThread.java
public class MainThread {
public static void main(String[] args) throws IOException, InterruptedException {
// 单文件测试
/*--------------------
URL url = new URL("https://imgsa.baidu.com/forum/w%3D580/sign=43e292947c1ed21b79c92eed9d6fddae/6bfab2fb43166d228b3c16f2472309f79052d20a.jpg");
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
OutputStream ot = new FileOutputStream(new File("C:\\Users\\Ensio\\Desktop\\HeiMa\\题库(2)\\题库\\robin\\目标数据\\6bfab2fb43166d228b3c16f2472309f79052d20a.jpg"));
byte[] bytes = new byte[1024];
int len;
while ((len = in.read(bytes)) != -1){
ot.write(bytes, 0, len);
}
in.close();
ot.close();
--------------------*/
// 实例化【图片地址库】,调用imgAdd 将本地【图片URL文件】读出,放入【图片地址库】中
ImgFiles img = new ImgFiles(imgAdd());
// 线程池初始化----------------->
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
10,
50,
5,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(10)
//Executors.defaultThreadFactory(),
//new ThreadPoolExecutor.AbortPolicy()
);
// 启动 50个 子线程 去执行任务
for (int i = 0; i < 50; i++) {
new Thread(new ThreadRunnable(img),i + 1 + "").start();
}
// 主线程等待
Thread.sleep(5000);
System.out.println("下载完成!");
// <--------------------------------
}
// 调用imgAdd 将本地【图片URL文件】读出,放入【图片地址库】中
public static ArrayList imgAdd() throws IOException {
// 本地【图片URL文件】读取
File file = new File("C:\\Users\\Ensio\\Desktop\\HeiMa\\题库(2)\\题库\\robin\\元数据\\img.txt");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
// 构建list,用于存放图片地址
String s;
ArrayList<String> list = new ArrayList<>();
while ((s = bufferedReader.readLine()) != null){
list.add(s);
}
return list;
}
}