該項目是基于某網頁上的圖片解析:主要利用了線程的同步與數據流的讀取技術.
public class Test02 {
public static void main(String[] args) {
// 創建一個圖片下載對象
final imgDownload imgDownload = new imgDownload();
//這部分代碼是創建了3個圖片鏈接
// 生產圖片鏈接
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i < 500; i++) {
try {
imgDownload.parseImageURL(i);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 500; i < 1000; i++) {
try {
imgDownload.parseImageURL(i);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1000; i < 1500; i++) {
try {
imgDownload.parseImageURL(i);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
//如下代碼是創建了5個下載圖片的線程
// 下載圖片
new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
imgDownload.downloadImage();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
imgDownload.downloadImage();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
imgDownload.downloadImage();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
imgDownload.downloadImage();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
imgDownload.downloadImage();
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}
class imgDownload {
// imgurl的數組
LinkedList<String> list = new LinkedList<String>();
// 解析鏈接
void parseImageURL(int page) throws Exception {
synchronized (this) {
if (list.size() > 100) {
wait();// 生產太多了,讓她等待 不知100個,會99+幾個
}
String htmlUrl = "https://www.doutula.com/article/list/?page="
+ page;
Document doc = Jsoup.connect(htmlUrl).get();
// 獲取圖片鏈接
Elements els = doc.select(".lazy,.image_dtb,.img-responsive");// css選擇器
Iterator iter = els.iterator();
while (iter.hasNext()) {
Element object = (Element) iter.next();
String imgUrl = object.attr("data-original");
if (!imgUrl.contains("http")) {
continue;
}
this.list.add(imgUrl);
}
// 通知可以下載圖片
notifyAll();
}
}
// 下載圖片
void downloadImage() throws Exception {
synchronized (this) {
if (this.list.size() <= 0) {
wait();
}
String imgUrl = null;
if (this.list.size()>0) {
imgUrl = this.list.removeFirst();
}else {
return;
}
//String imgUrl = this.list.removeFirst();
// 又要取又要拿 返回的是刪掉的元素
URL url = new URL(imgUrl);// 請求資源
URLConnection con = url.openConnection();// 獲取鏈接
con.connect();// 去鏈接
InputStream stream = con.getInputStream();// 獲取流
byte[] by = new byte[1024];
int length = -1;
// 寫入流 out流
String[] imageURLArr = imgUrl.split("/");
String fileName = "img/" + imageURLArr[imageURLArr.length - 1];
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
while ((length = stream.read(by)) != -1) {
// 把流寫入到本地文件夾
fileOutputStream.write(by, 0, length);
}
fileOutputStream.close();
stream.close();
if (this.list.size() < 100) {
notify();
}
}
}
}