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+"个图片。");
}
}

java版模拟浏览器下载百度动漫图片到本地。的更多相关文章

  1. [JAVA]解决不同浏览器下载附件的中文名乱码问题

    附件下载时,遇到中文附件名的兼容性问题,firefox.chrome.ie三个派系不兼容,通过分析整理,总结出处理该问题的办法,记录如下: 1.文件名编码 服务器默认使用的是ISO8859-1,而我们 ...

  2. java 实现模拟浏览器 访问网站

    一般的情况下我们都是使用IE或者Navigator浏览器来访问一个WEB服务器,用来浏览页面查看信息或者提交一些数据等等.所访问的这些页面 有的仅仅是一些普通的页面,有的需要用户登录后方可使用,或者需 ...

  3. Java 使用IE浏览器下载文件,文件名乱码问题

    使用Servlet实现文件下载功能时,使用IE下载出现文件名乱码; 网上常见的解决办法是通过"user-agen"来判断浏览器: if (req.getHeader("u ...

  4. Java 解决IE浏览器下载文件,文件名出现乱码问题

    /** * 区分ie 和其他浏览器的下载文件乱码问题 * @param request * @param fileName * @return */ public String getFileName ...

  5. Java使用IE浏览器下载文件,文件名乱码问题

    String userAgent = request.getHeader("user-agent").toLowerCase(); if (userAgent.contains(& ...

  6. 浏览器下载img标签Base64图片

    https://blog.csdn.net/qq_42076140/article/details/82113622    原文地址 <a href="javascript:downl ...

  7. java读流方式,下载网络上的图片

    本工具类支持url的list集合,具体实现如下所示: public static void download(ArrayList<String> listUrl, String downl ...

  8. 下载百度上的图片C#——输入名字就可以下载

    using System; using System.Collections.Generic; using System.Data; using System.Configuration; using ...

  9. python爬虫:使用Selenium模拟浏览器行为

    前几天有位微信读者问我一个爬虫的问题,就是在爬去百度贴吧首页的热门动态下面的图片的时候,爬取的图片总是爬取不完整,比首页看到的少.原因他也大概分析了下,就是后面的图片是动态加载的.他的问题就是这部分动 ...

随机推荐

  1. table sorting–angularjs

    1: <script type="text/javascript" ng:autobind 2: src="http://code.angularjs.org/0. ...

  2. 浅析mongodb中group分组

    这篇文章主要介绍了浅析mongodb中group分组的实现方法及示例,非常的简单实用,有需要的小伙伴可以参考下. group做的聚合有些复杂.先选定分组所依据的键,此后MongoDB就会将集合依据选定 ...

  3. 李洪强iOS经典面试题142-第三方框架及其管理

    李洪强iOS经典面试题142-第三方框架及其管理   第三方框架及其管理 使用过CocoaPods吗?它是什么?CocoaPods的原理? CocoaPod是一个第三方库的管理工具,用来管理项目中的第 ...

  4. 【Alpha】Daily Scrum Meeting第八次

    一.本次Daily Scrum Meeting主要内容 抓紧冲刺(接下去两天都在下午增加一个小会议) 剩余任务的概况 二.项目进展 学号尾数 今日已完成任务 接下去要做 502 无 将数据库的数据转换 ...

  5. 重学ps_1

    1,选取 打开图片->点击选取工具->ctrl+c->ctrl+n->ctrl+v 2,去除图片背景 打开你要去除背景的图片->在图层面板中->双击图层把图层改为0 ...

  6. 页面引入flash

    function shFlashObj(id, data, oWidth, oHeight, flashvals,beFullScreen) {    var swf='<object id=& ...

  7. Maven项目WEB-INF/views无法引入js,css静态文件解决方法

    web.xml针对文件后缀配置以下,对客户端请求的静态资源如图片.JS文件等的请求交由默认的servlet进行处理 <servlet-mapping> <servlet-name&g ...

  8. ExtJS笔记 Ext.data.Types

    This is a static class containing the system-supplied data types which may be given to a Field. Type ...

  9. ThinkPHP数据库访问CRUD;__SELF__和__ACTION__的区别;自动收集表单:$n->create();

    一.tp框架数据访问(pdo基础) public function test() { $n = D("Nation"); //select();find(); //查询 1.$at ...

  10. JavaScript中一些常用的方法整理

    当前时间和输入时间比较 var timeLong = Date.parse(new Date());//当前时间var t1 = Date.parse($("#returnTime2&quo ...