還不夠完美,只能下載樓主上傳的圖片,樓主如果是盜鏈的則無法識別,還有的流的開閉太頻繁了.
import java.io.BufferedReader;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.URL;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * 批量下載百度貼吧樓主上傳的圖片 * 如果圖片是樓主使用連結地址則無法下載 * 這時可以重寫正則匹配原則 * @author GeenkEmp01 * */public class GetBaiDuTieBaPicture {private int index = 0;/** * @param args */public static void main(String[] args) {GetBaiDuTieBaPicture getP = new GetBaiDuTieBaPicture();String tieziUrl = "http://tieba.baidu.com/p/1212071711";String imageDirectory = "E:/baiduimage/";getP.getImageUrl(tieziUrl,imageDirectory);}/** * * @param tieziUrl * 文章的網址 * @param imageDirectory * 儲存下載圖片的目錄 */public void getImageUrl(String tieziUrl, String imageDirectory) {int pn = getTotalPageNum(tieziUrl);URL url;Pattern p = Pattern.compile("http://imgsrc.baidu.com/forum/pic/item/[\\w,\\d]{40}.jpg");InputStream is = null;BufferedReader br = null;for (int i = 1; i <= pn; i++) {try {url = new URL(tieziUrl+"?pn=" + i);is = (InputStream) url.getContent();br = new BufferedReader(new InputStreamReader(is));String str = null;while ((str = br.readLine()) != null) {Matcher m = p.matcher(str);while (m.find()) {index++;String imageUrl = m.group();System.out.println(imageUrl);System.out.println("正在下載第" + index + "張圖片...");downloadImage(imageUrl, imageDirectory + index + ".jpg");}}} catch (Exception e) {e.printStackTrace();} finally {try {if (br != null)br.close();if (is != null)is.close();} catch (IOException e) {e.printStackTrace();}}}System.out.println("共下載了" + index + "張圖片");}/** * 擷取文章總共頁數 * @param tieziUrl * @return */public int getTotalPageNum(String tieziUrl) {int pageNum = 1;URL url;Pattern p = Pattern.compile("pn=\\d*\">尾頁<");InputStream is = null;BufferedReader br = null;try {url = new URL(tieziUrl);is = (InputStream) url.getContent();br = new BufferedReader(new InputStreamReader(is));String str = null;while ((str = br.readLine()) != null) {Matcher m = p.matcher(str);while (m.find()) {String s = m.group();pageNum = Integer.parseInt(s.substring(3, s.length()-5));}}} catch (Exception e) {e.printStackTrace();} finally {try {if (br != null)br.close();if (is != null)is.close();} catch (IOException e) {e.printStackTrace();}}System.out.println("文章共有"+pageNum+"頁");return pageNum;}/** * 下載一張圖片 * @param imageUrl * @param saveFile */public void downloadImage(String imageUrl, String saveFile) {URL url = null;OutputStream os = null;InputStream is = null;try {url = new URL(imageUrl);is = url.openStream();os = new FileOutputStream(saveFile);byte[] buff = new byte[1024];int readed;while ((readed = is.read(buff)) != -1) {os.write(buff, 0, readed);}} catch (Exception e) {e.getStackTrace();} finally {try {if (is != null)is.close();if (os != null)os.close();} catch (IOException e) {e.printStackTrace();}}}}