Copy codeThe Code is 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 image address and download the image
Public void getHtmlPicture (String httpUrl ){
URL url;
BufferedInputStream in;
FileOutputStream file;
Int count; // the serial number of the image file name.
FileNumber num = new FileNumber (); // class of the serial number of the image file name. num is the object.
Count = num. NumberReadFromFile (); // obtain the serial number of the image file.
Try {
System. out. println ("getting network images ");
String fileName = (String. valueOf (count )). concat (httpUrl. substring (httpUrl. lastIndexOf (". "); // The suffix of the part added to the serial number of the image file. The suffix is obtained using a method in String.
// HttpUrl. substring (httpUrl. lastIndexOf ("/"); // The obtained file name is the image name in the image link.
String filePath = "d:/image/"; // image storage location
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 ("image retrieved successfully ");
Count = count + 1; // the serial number of the image file plus 1
Num. NumberWriteToFile (count); // Save the image name number
} Catch (MalformedURLException e ){
E. printStackTrace ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
// Obtain the webpage code and save it to the String-format Content.
Public String getHtmlCode (String httpUrl) throws IOException {
String content = "";
URL uu = new URL (httpUrl); // create a URL Class Object
BufferedReader ii = new BufferedReader (new InputStreamReader (uu
. OpenStream (); // use openStream to obtain an input stream and construct a BufferedReader object.
String input;
While (input = ii. readLine ())! = Null) {// create a read loop and determine whether a read value exists.
Content + = input;
}
Ii. close ();
Return content;
}
// Analyze the webpage code and find the matched webpage image address
Public void get (String url) throws IOException {
String searchImgReg = "(? X) (src | SRC | background | BACKGROUND) = ('| \")/? ([\ W-] +/) * ([\ w-] + \\. (jpg | JPG | png | PNG | gif | GIF) ('| \ ")"; // used to find matching image links in the webpage 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 indicates the object gcp, which calls the getHtmlCode method to obtain the webpage code.
// System. out. println (content); // The output content is a continuous 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 the image link address to the screen.
// System. out. println (url );
This. getHtmlPicture (matcher. group (3); // The object calls getHtmlPicture to download the image file from the Internet and output it 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 webpage of the main function url
Public static void main (String [] args) throws IOException {
String url = "http://www.baidu.com ";
GetContentPicture gcp = new GetContentPicture ();
Gcp. get (url );
}
}
Copy codeThe Code is as follows: package cn. mypic;
Import java. io .*;
Public class FileNumber {
// File write
Public void NumberWriteToFile (int x ){
Int c = 0;
C = x;
File filePath = new File ("d:/image"); // File name No. TXT File storage 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 the file content to the screen
}
Catch (FileNotFoundException e ){
System. err. println (e );
}
Catch (IOException e ){
System. err. println (e );
}
Return c1;
}
Public static void main (String args []) {
}
}