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. js 封装设计cookie

    http://www.imooc.com/article/12700<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/ ...

  2. css input[type=file] 样式美化,input上传按钮美化

    css input[type=file] 样式美化,input上传按钮美化 参考:http://www.haorooms.com/post/css_input_uploadmh

  3. grunt 检测js配置

    module.exports = function(grunt) { // 项目配置 grunt.initConfig({ pkg: grunt.file.readJSON('package.json ...

  4. BZOJ1068: [SCOI2007]压缩

    ... 1068: [SCOI2007]压缩 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 909  Solved: 566[Submit][Statu ...

  5. worldwind一些资料

    worldwind一些资料: http://blog.csdn.net/jk276993857/article/category/710116 http://blog.csdn.net/paul_xj ...

  6. this用法(ryf)

    this是javascript语言的一个关键字 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用,比如: function test(){ this.x = 1; } 随着函数使用场合的不 ...

  7. Html笔记

    1. Html就是超文本标记语言的简写,是最基础的网页语言. 2. Html是通过标签来定义的语言,代码都是由标签所组成. 3. Html代码不用区分大小写. 4. Html代码由<html&g ...

  8. poj1700-Crossing River(贪心算法)

    一,题意: 只有一艘船,能乘2人,船的运行时间为2人中较多一人的时间,过去后还需一个人把船划回来,问把n个人运到对岸,最少需要多久.二,思路步骤: 想办法先把用时最多的2人,运过去,再把剩下来的人中用 ...

  9. Java集合类学习-LinkedList, ArrayList, Stack, Queue, Vector

    Collection List 在Collection的基础上引入了有序的概念,位置精确:允许相同元素.在列表上迭代通常优于索引遍历.特殊的ListIterator迭代器允许元素插入.替换,双向访问, ...

  10. Win7 下安装VirtualBox 没有Ubuntu 64bit 选项问题

    参考: win7安装virtualbox遇到的问题 基于VirtualBox虚拟机安装Ubuntu图文教程 问题 在安装VirtualBox之后,选择虚拟机进行安装的时候发现没有Ubuntu 64bi ...