实现Runable接口,重写run方法,new Thread(类对象).start();
**
public class commons extends Thread{
//run是线程的入口点
public void download(String url,String name)
{
try {
FileUtils.copyURLToFile(new URL(url), new File(name));
} catch (MalformedURLException e) {
e.printStackTrace();
System.out.println("不合法的路径");
} catch (IOException e) {
e.printStackTrace();
System.out.println("图片下载失败");
}
}
}
//开启下载:
public class ThreadDownload extends Thread {
private String url; //远程路径
private String name; //存储名字
public ThreadDownload(String url,String name)
{
this.url=url;
this.name=name;
}
public void run()
{
commons wd=new commons();
wd.download(url, name);
}
public static void main(String[]args)
{
ThreadDownload td =new ThreadDownload("http://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&ie=utf-8&in=24401&cl=2&lm=-1&st=-1&step_word=&rn=1&cs=&ln=1998&fmq=1402900904181_R&ic=0&s=&se=1&sme=0&tab=&width=&height=&face=0&is=&istype=2&ist=&jit=&fr=ala&ala=1&alatpl=others&pos=1&pn=3&word=%E5%9B%BE%E7%89%87%20%E5%8A%A8%E6%BC%AB%E5%8D%A1%E9%80%9A&di=213510&os=4129833545,253122553&pi=0&objurl=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201209%2F02%2F20120902160411_ik2UH.jpeg","D:/d/a.jpg");
ThreadDownload td2=new ThreadDownload("http://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&ie=utf-8&in=24401&cl=2&lm=-1&st=-1&step_word=&rn=1&cs=&ln=1998&fmq=1402900904181_R&ic=0&s=&se=1&sme=0&tab=&width=&height=&face=0&is=&istype=2&ist=&jit=&fr=ala&ala=1&alatpl=others&pos=1&pn=2&word=%E5%9B%BE%E7%89%87%20%E5%8A%A8%E6%BC%AB%E5%8D%A1%E9%80%9A&di=237600&os=1039188367,1392108594&pi=0&objurl=http%3A%2F%2Fimg.alicdn.com%2Fimgextra%2Fi1%2F3498538608%2FTB2BY1Be8HH8KJjy0FbXXcqlpXa_!!3498538608-0-daren.jpg","D:/d/b.jpg");
ThreadDownload td3=new ThreadDownload("http://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&ie=utf-8&in=24401&cl=2&lm=-1&st=-1&step_word=&rn=1&cs=&ln=1998&fmq=1402900904181_R&ic=0&s=&se=1&sme=0&tab=&width=&height=&face=0&is=&istype=2&ist=&jit=&fr=ala&ala=1&alatpl=others&pos=1&pn=0&word=%E5%9B%BE%E7%89%87%20%E5%8A%A8%E6%BC%AB%E5%8D%A1%E9%80%9A&di=83710&os=1459185365,1117709152&pi=0&objurl=http%3A%2F%2Fimg.52z.com%2Fupload%2Fnews%2Fimage%2F20180129%2F20180129082229_14849.jpg","c.jpg");
//启动三个线程
td.start();
td2.start();
td3.start();
}
}
原文:https://blog.51cto.com/14437184/2426622