菜鸟我自己花了3个小时写的代码,没怎么组织,就自己看得懂了
小程序的功能是: 下载某个网站的图片,大大的福利啊,好了,直接上代码了,也方便以后自己修改改善
主程序入口
package com.sise.Rola;
import java.io.File;
import java.util.List;
public class MyRobot {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File dir = new File("d://image//");
if(!dir.exists()) {
dir.mkdir();
}
String topUrl = "http://sexy.faceks.com/";
GetUrls getUrls = new GetUrls();
List urls = getUrls.getUrls(topUrl);
//创建一个可重用固定线程数的线程池
//ExecutorService pool = Executors.newFixedThreadPool(3);
for(String it : urls) {
new Thread(new ImageDownload(it)).start();
}
}
}
获取下载的url
package com.sise.Rola;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
*取得待下载的url
**/
public class GetUrls {
public List urls = new ArrayList();
public List getUrls(String topUrl) {
try {
Document doc = Jsoup.connect(topUrl).timeout(5000).get();
Elements links = doc.select("a.img");
for(int i=0;i
Element element = links.get(i);
urls.add(element.attr("href"));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return urls;
}
//public static void main(String[] args) {
//GetUrls get = new GetUrls();
//get.getUrls("http://sexy.faceks.com");
//}
}
package com.sise.Rola;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
*根据取得的url下载图片
**/
public class ImageDownload implements Runnable{
private String currentUrl;
private List imageList = new ArrayList();
public ImageDownload(String url) {
this.currentUrl = url;
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("线程 "+Thread.currentThread().getName() +" 开始下载福利(你懂的- -!).....");
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Document doc = Jsoup.connect(currentUrl).timeout(10000).get();
Elements links = doc.select("a.img > img");
for(int i=0;i
Element element = links.get(i);
imageList.add(element.attr("src"));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//开始下载图片
CloseableHttpClient httpClient = HttpClients.createDefault();
for(String it : imageList) {
HttpGet get = new HttpGet(it);
try {
HttpResponse response = httpClient.execute(get);
long name = new Date().getTime();
File image = new File("D://image//"+name+".jpg");
FileOutputStream output = new FileOutputStream(image);
HttpEntity entity = response.getEntity();
entity.writeTo(output);
output.flush();
output.close();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
httpClient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("线程 "+Thread.currentThread().getName()+" 下载完成!!!!");
}
}