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
| public class TestThread02 extends Thread{
private String url; private String name;
private TestThread02(String url, String name) { this.url = url; this.name = name; }
@Override public void run() { WebDownload webDownload = new WebDownload(); webDownload.download(url, name); System.out.println("下载了文件名为:" + name); }
public static void main(String[] args) { TestThread02 t1 = new TestThread02("https://mmbiz.qpic.cn/mmbiz_jpg/uChmeeX1FpyB6WkMTL2IUapfTtGH6FFOialcoD9IRXH5jUskMiaHfPd8AKmWZYGAerLH3bpulF72IYhrK2kuia9Pg/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1","1.png"); TestThread02 t2 = new TestThread02("https://mmbiz.qpic.cn/mmbiz_jpg/uChmeeX1FpyB6WkMTL2IUapfTtGH6FFOialcoD9IRXH5jUskMiaHfPd8AKmWZYGAerLH3bpulF72IYhrK2kuia9Pg/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1","2.png"); TestThread02 t3 = new TestThread02("https://mmbiz.qpic.cn/mmbiz_jpg/uChmeeX1FpyB6WkMTL2IUapfTtGH6FFOialcoD9IRXH5jUskMiaHfPd8AKmWZYGAerLH3bpulF72IYhrK2kuia9Pg/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1","3.png");
t1.start(); t2.start(); t3.start(); } }
class WebDownload { public void download(String url, String name){ try { FileUtils.copyURLToFile(new URL(url), new File(name)); } catch (IOException e) { e.printStackTrace(); System.out.println("IO异常,download方法出现问题"); } } }
|