package cn.mypic;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GetContentPicture {
//得到了圖片地址並下載圖片
public void getHtmlPicture(String httpUrl) {
URL url;
BufferedInputStream in;
FileOutputStream file;
int count; //圖片文件名序號
FileNumber num=new FileNumber();//圖片文件名序號類,num為對象
count=num.NumberReadFromFile();//獲取圖片文件序號
try {
System.out.println("獲取網絡圖片");
String fileName = (String.valueOf(count)).concat(httpUrl.substring(httpUrl.lastIndexOf(".")));//圖片文件序號加上圖片的後綴名,後綴名用了String內的一個方法來獲得
//httpUrl.substring(httpUrl.lastIndexOf("/"));//這樣獲得的文件名即是圖片鏈接裡圖片的名字
String filePath = "d:/image/";//圖片存儲的位置
url = new URL(httpUrl);
in = new BufferedInputStream(url.openStream());
file = new FileOutputStream(new File(filePath+fileName));
int t;
while ((t = in.read()) != -1) {
file.write(t);
}
file.close();
in.close();
System.out.println("圖片獲取成功");
count=count+1;//圖片文件序號加1
num.NumberWriteToFile(count);//將圖片名序號保存
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//獲取網頁的代碼保存在String格式的Content中
public String getHtmlCode(String httpUrl) throws IOException {
String content ="";
URL uu = new URL(httpUrl); // 創建URL類對象
BufferedReader ii = new BufferedReader(new InputStreamReader(uu
.openStream())); // //使用openStream得到一輸入流並由此構造一個BufferedReader對象
String input;
while ((input = ii.readLine()) != null) { // 建立讀取循環,並判斷是否有讀取值
content += input;
}
ii.close();
return content;
}
//分析網頁代碼,找到匹配的網頁圖片地址
public void get(String url) throws IOException {
String searchImgReg = "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";//用於在網頁代碼Content中查找匹配的圖片鏈接。
String searchImgReg2 = "(?x)(src|SRC|background|BACKGROUND)=('|\")(http://([\\w-]+\\.)+[\\w-]+(:[0-9]+)*(/[\\w-]+)*(/[\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";
String content = this.getHtmlCode(url);//this指對象gcp,在此地調用獲取網頁代碼,getHtmlCode方法
//System.out.println(content); //輸出的content將是一個連續的字符串。
Pattern pattern = Pattern.compile(searchImgReg);//java.util.regex.Pattern
Matcher matcher = pattern.matcher(content); //java.util.regex.Matcher
while (matcher.find()) {
System.out.println(matcher.group(3));//輸出圖片鏈接地址到屏幕
// System.out.println(url);
this.getHtmlPicture(matcher.group(3));//對象調用getHtmlPicture從網上下載並輸出圖片文件到指定目錄
}
pattern = Pattern.compile(searchImgReg2);
matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println(matcher.group(3));
this.getHtmlPicture(matcher.group(3));
}
// searchImgReg =
// "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";
}
//主函數url網頁的地址
public static void main(String[] args) throws IOException {
String url = "http://www.baidu.com";
GetContentPicture gcp = new GetContentPicture();
gcp.get(url);
}
}
Java代碼
package cn.mypic;
import java.io.*;
public class FileNumber{
//文件寫
public void NumberWriteToFile(int x){
int c=0;
c=x;
File filePath=new File("d:/image");//文件名序號TXT文件保存地址
File f1=new File(filePath,"number.txt");
try{
FileOutputStream fout=new FileOutputStream(f1);
DataOutputStream out=new DataOutputStream(fout);
out.writeInt(c);
}
catch(FileNotFoundException e){
System.err.println(e);
}
catch(IOException e){
System.err.println(e);
}
}
//文件讀
public int NumberReadFromFile(){
int c1 = 0;
File filePath=new File("d:/image");
File f1=new File(filePath,"number.txt");
try{
FileInputStream fin=new FileInputStream(f1);
DataInputStream in=new DataInputStream(fin);
c1=in.readInt();
System.out.println(c1);//輸出文件內容至屏幕
}
catch(FileNotFoundException e){
System.err.println(e);
}
catch(IOException e){
System.err.println(e);
}
return c1;
}
public static void main(String args[]){
}
}