Copy Code code as follows:
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 {
Get the picture address and download the picture
public void Gethtmlpicture (String httpurl) {
URL url;
Bufferedinputstream in;
FileOutputStream file;
int count; Picture filename number
FileNumber num=new filenumber ()//Picture file name ordinal class, num as Object
Count=num. Numberreadfromfile ()//Get picture file number
try {
SYSTEM.OUT.PRINTLN ("Get network Picture");
String FileName = (string.valueof (count)). Concat (Httpurl.substring (Httpurl.lastindexof (".")); /Picture file number plus the name of the suffix of the picture, the suffix is used in a string of a method to obtain
Httpurl.substring (Httpurl.lastindexof ("/"))//This gets the filename that is the name of the picture in the picture link
String FilePath = "d:/image/";//location of picture storage
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 ("Picture acquisition success");
count=count+1;//picture file serial number plus 1
Num. Numberwritetofile (count);//Save Picture name serial number
catch (Malformedurlexception e) {
E.printstacktrace ();
catch (FileNotFoundException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}
The code to get the Web page is stored in the content in string format
public string Gethtmlcode (String httpurl) throws IOException {
String content = "";
URL uu = new URL (httpurl); Create a URL class object
BufferedReader II = new BufferedReader (UU, New InputStreamReader
. OpenStream ()); Use OpenStream to get an input stream and thus construct a BufferedReader object
String input;
while (input = Ii.readline ())!= null) {//Establish a read loop and determine if there are read values
Content + + input;
}
Ii.close ();
return content;
}
Analyze the page code to find a matching page image address
public void get (String url) throws IOException {
String Searchimgreg = "(? x) (src| src|background| BACKGROUND) = (' |\ ')/? ([\\w-]+/] * ([\\w-]+\\. ( Jpg| Jpg|png| Png|gif| GIF))) (' |\ '),//is used to find matching picture links in the page code 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 refers to Object GCP, where you invoke the code to get the Web page, Gethtmlcode method
SYSTEM.OUT.PRINTLN (content); The content of the output will be a contiguous string.
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))//Output picture link address to screen
System.out.println (URL);
This.gethtmlpicture (Matcher.group (3))//object invoke gethtmlpicture download and output picture files from the Web to the specified directory
}
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)))) (' |\ ') ";
}
Address of the main function URL Web page
public static void Main (string[] args) throws IOException {
String url = "Http://www.baidu.com";
Getcontentpicture GCP = new Getcontentpicture ();
Gcp.get (URL);
}
}
Copy Code code as follows:
Package cn.mypic;
Import java.io.*;
public class filenumber{
File Write
public void Numberwritetofile (int x) {
int c=0;
C=x;
FileName filepath=new file ("D:/image"),//file name ordinal txt files save address
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);
}
}
File read
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)//Output file content to screen
}
catch (FileNotFoundException e) {
System.err.println (e);
}
catch (IOException e) {
System.err.println (e);
}
return C1;
}
public static void Main (String args[]) {
}
}