java版模拟浏览器下载百度动漫图片到本地。

时间:2023-03-09 03:17:48
java版模拟浏览器下载百度动漫图片到本地。
 package javaNet.Instance.ImageDownload;

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class DownloadImgs { private String url=null; public DownloadImgs(String url) {
this.url=url;
} //----------------------------------gethtml start-----------------------------
/**
* visit the baidu.img page to get the html
* @return inputStream
* @throws IOException
* @throws MalformedURLException
*/
public InputStream GetBaiduImgHtml_Stream() throws IOException,MalformedURLException {
URL img_Url=new URL(url);
return img_Url.openStream();
} /**
* convert the stream to the string
* @param inStrm
* @return string of the page
*/
public String InputStreamToString(InputStream inStrm){
BufferedReader reader=new BufferedReader(new InputStreamReader(inStrm));
StringBuilder sb=new StringBuilder(); String line=null; try {
while((line=reader.readLine())!=null){
sb.append(line+'\n');
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
inStrm.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return sb.toString();
} /**
* get the origin page of baidu.img
* @return
* @throws MalformedURLException
* @throws IOException
*/
public String GetBaiduImgHtml_Page() throws MalformedURLException, IOException
{
return this.InputStreamToString(this.GetBaiduImgHtml_Stream());
}
/**
* test whether url have been visited the image page,and get the page.
* @param page
*/
public void Display_HtmlPage(String page)
{
System.out.println(page);
}
//-------------------------gethtml end----------------
//-------------------------paretoimgurllist start-----
public ArrayList<String> ParsePageToImgList(String page,String imgPa)
{
ArrayList<String> imgList=new ArrayList<String>();
Pattern pattern=Pattern.compile(imgPa);
Matcher matcher=pattern.matcher(page);
while(matcher.find())
{
imgList.add(matcher.group(1));
}
return imgList;
}
//------------------------paretoimgurllist end---------
//------------------------DownloadFile start----------
public boolean DownloadFile(String imgUrl,int index,String path)
{
try
{
File f=new File(path+"\\"+index+".jpg");
System.out.println("下载:"+imgUrl);
URL url=new URL(imgUrl);
InputStream ins=url.openStream();
FileOutputStream fout=new FileOutputStream(f);
byte[] buffer=new byte[2048];
int bytes_number;
while((bytes_number=ins.read(buffer))!=-1)
{
fout.write(buffer,0,bytes_number);
fout.flush();
}
ins.close();
fout.close();
}
catch(Exception e)
{
System.out.println("下载失败!");
e.printStackTrace();
return false;
}
System.out.println("下载完成...");
return true;
}
//------------------------DownloadFile end---------- //------------------------mkDir start----------
/**
* make a direction for download the images in the native disk.
* @param path the native path
* @return is success
*/
public void MkDir(String path)
{
File dir=new File(path);
if(!dir.exists())
{
dir.mkdirs();
}
}
//------------------------mkDir end------------ public void Display_ArrayList(ArrayList<String> list)
{
for(String temp:list)
{
System.out.println(temp);
}
} public static void main(String[] args) throws MalformedURLException, IOException
{
String imgPa="\"objURL\":\"(.*?)\"";
String path="F:\\photos";
int index=0;
DownloadImgs downloadimgs=new DownloadImgs("http://image.baidu.com/search/index?"
+ "tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=index&fr=&sf=1"
+ "&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0"
+ "&istype=2&ie=utf-8&word=%E5%8A%A8%E6%BC%AB&oq=%E5%8A%A8%E6%BC%AB&rsp=-1");
//downloadimgs.Display_HtmlPage(downloadimgs.GetBaiduImgHtml_Page());
String htmlPage=downloadimgs.GetBaiduImgHtml_Page();
ArrayList<String> imgList=downloadimgs.ParsePageToImgList(htmlPage, imgPa);
//downloadimgs.Display_ArrayList(imgList);
downloadimgs.MkDir(path);
for(String imgUrl:imgList)
downloadimgs.DownloadFile(imgUrl, (index++)+1, path); System.out.println("一共下载了"+index+"个图片。");
}
}