hooyantsing's Blog

4_网图下载

字数统计: 239阅读时长: 1 min
2020/08/20

4 网图下载

狂神说Java 多线程详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class TestThread2 extends Thread{
private String url;
private String name;

public TestThread2(String url, String name) {
this.url = url;
this.name = name;
}

@Override
public void run() {
new WebDownloader().downloader(url,name);
System.out.println("下载了文件名:" + name);
}

public static void main(String[] args) {
TestThread2 t1 = new TestThread2("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=151472226,3497652000&fm=26&gp=0.jpg","1.jpg");
TestThread2 t2 = new TestThread2("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3367568658,3302321631&fm=26&gp=0.jpg","2.jpg");
TestThread2 t3 = new TestThread2("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1597910837734&di=c04bbb658aae00cadceece45f98c7bf6&imgtype=0&src=http%3A%2F%2F01.imgmini.eastday.com%2Fmobile%2F20170709%2F20170709004858_f316b2aee3d7cc6a0408be4dd5c633e3_1.jpeg","3.jpg");

t1.start();
t2.start();
t3.start();
}
}

class WebDownloader {
// 下载方法
public void downloader(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO 异常");
}
}
}

执行结果:

654903f90aa1f7cc9810ac5e2fd0d639.png

04e7b049ea55b39c593ea96b5440c286.png

CATALOG
  1. 1. 4 网图下载