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. div可编辑状态设置

    <div contentedittable="ture"></div>

  2. [BZOJ1501][NOI2005] 智慧珠游戏

    Input 文件中包含初始的盘件描述,一共有10行,第i行有i个字符.如果第i行的第j个字符是字母”A”至”L”中的一个,则表示第i行第j列的格子上已经放了零件,零件的编号为对应的字母.如果第i行的第 ...

  3. 使用JS实现图片展示瀑布流效果

    不知大家有没有发现,一般的图片展示网站都会使用瀑布流效果,所谓的瀑布流 就是网站内的图片不会一下子全缓存出来,而是等你滚动到一定的距离的时候, 下面的图片才会继续缓存,并且图片也是随机出现的,只是宽度 ...

  4. Android-Sqlite数据库的操作

    Sqlite数据库的简单操作: 设置增删改查的按钮,xml界面布局设置 <?xml version="1.0" encoding="utf-8"?> ...

  5. JsonTest

    以前用MVC写网站时并不用考虑Json的转换,MVC已经提供了现成的方法. 现在没有用MVC,我就在考虑如何自己转换Json,想来想去自己写还是不够完美,于是尝试了一些其他人写的方法,尝试过微软提供的 ...

  6. 怎么使用CKEDITOR

    出于工作需求,自己在网上找了个文本编辑器控件, 网址是http://ckeditor.com/ 怎么使用? 先插入脚本<script type="text/javascript&quo ...

  7. linux引导流程

    本章重点: 1.linux引导流程 2.linux运行级别 3.linux启动服务管理 4.GRUB配置与应用 5.启动故障分析解决 linux启动流程 1.固件(fireware):固话在硬件上的程 ...

  8. HTML基础篇之列表相关标签和特殊字符实体

    HTML字符实体 常用的字符实体: 实体字符 字符实体 大于号 (>) > 小于号 (<) < 引号 (") " 注册商标(®) ® 版权(© ) © &a ...

  9. mysql求最大第二,最大第三个数

    题目是这样的: 集团有多个部门,部门下有多个员工,求每个部门绩效排名第二的人员 sql语句是这样的 SELECT dep, MAX(score) FROM zx WHERE score NOT IN ...

  10. InnoDB杂记

    一.InnoDB写数据流程(猜想) myisam是将索引放入内存缓存(Key Cache,大小有key_buffer_size设置) innodb时间索引和数据文件都放入内存缓存池(Buffer Po ...