【搜索引擎Jediael开发笔记2】使用HttpClient下载网页至本地文件
本文使用HttpClient根据url进行网页下载。其中
(1)HttpClient的相关知识请参见HttpClient基础教程
(2)
package org.ljh.search.downloadpage; import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Scanner; import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; //本类用于将指定url对应的网页下载至本地一个文件。
public class PageDownloader { public static void downloadPageByGetMethod(String url) throws IOException { // 1、通过HttpGet获取到response对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 注意,必需要加上http://的前缀,否则会报:Target host is null异常。
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet); InputStream is = null;
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
try {
// 2、获取response的entity。
HttpEntity entity = response.getEntity(); // 3、获取到InputStream对象,并对内容进行处理
is = entity.getContent(); String fileName = getFileName(url);
saveToFile("D:\\tmp\\", fileName, is);
} catch (ClientProtocolException e) {
e.printStackTrace();
} finally { if (is != null) {
is.close();
}
if (response != null) {
response.close();
}
}
}
} //将输入流中的内容输出到path指定的路径,fileName指定的文件名
private static void saveToFile(String path, String fileName, InputStream is) {
Scanner sc = new Scanner(is);
Writer os = null;
try {
os = new PrintWriter(path + fileName);
while (sc.hasNext()) {
os.write(sc.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sc != null) {
sc.close();
}
if (os != null) {
try{
os.flush();
os.close();
}catch(IOException e){
e.printStackTrace();
System.out.println("输出流关闭失败!");
}
}
}
} // 将url中的特殊字符用下划线代替
private static String getFileName(String url) {
url = url.substring(7);
String fileName = url.replaceAll("[\\?:*|<>\"/]", "_") + ".html";
return fileName;
} }
【搜索引擎Jediael开发笔记2】使用HttpClient下载网页至本地文件的更多相关文章
- 【搜索引擎Jediael开发笔记】v0.1完整代码
详细代码请见 E:\Project\[重要]归档代码\SearchEngine归档代码 或 https://code.csdn.net/jediael_lu/jediael/tree/10991c83 ...
- 【搜索引擎Jediael开发笔记1】搜索引擎初步介绍及网络爬虫
详细可参考 (1)书箱:<这就是搜索引擎><自己动手写网络爬虫><解密搜索引擎打桩实践> (2)[搜索引擎基础知识1]搜索引擎的技术架构 (3)[搜索引擎基础知识2 ...
- 【搜索引擎Jediael开发笔记】v0.1完整代码 2014-05-26 15:17 463人阅读 评论(0) 收藏
详细代码请见 E:\Project\[重要]归档代码\SearchEngine归档代码 或 https://code.csdn.net/jediael_lu/jediael/tree/10991c83 ...
- 【搜索引擎Jediael开发笔记】V0.1完整代码 2014-05-26 15:16 443人阅读 评论(0) 收藏
详细代码请见 E:\Project\[重要]归档代码\SearchEngine归档代码 或 https://code.csdn.net/jediael_lu/jediael/tree/10991c83 ...
- 【搜索引擎Jediael开发笔记3】使用HtmlParser提取网页中的链接
关于HtmpParser的基本内容请见 HtmlParser基础教程 本文示例用于提取HTML文件中的链接 package org.ljh.search.html; import java.util. ...
- 【搜索引擎Jediael开发4】V0.01完整代码
截止目前,已完成如下功能: 1.指定某个地址,使用HttpClient下载该网页至本地文件 2.使用HtmlParser解释第1步下载的网页,抽取其中包含的链接信息 3.下载第2步的所有链接指向的网页 ...
- 【搜索引擎Jediael开发4】V0.01完整代码 分类: H_HISTORY 2014-05-21 21:35 470人阅读 评论(0) 收藏
截止目前,已完成如下功能: 1.指定某个地址,使用HttpClient下载该网页至本地文件 2.使用HtmlParser解释第1步下载的网页,抽取其中包含的链接信息 3.下载第2步的所有链接指向的网页 ...
- TERSUS无代码开发(笔记01)-按装下载和基础语法
1.中国官网 https://tersus.cn/ 2.下载:https://tersus.cn/download/ 3.开发文档:https://tersus.cn/docs/ 4.基本元件说明 图 ...
- Java开发笔记(九十五)NIO配套的文件工具Files
NIO不但引进了高效的文件通道,而且新增了更加好用的文件工具家族,包括路径组工具Paths.路径工具Path.文件组工具Files.先看路径组工具Paths,该工具提供了静态方法get,输入某个文件的 ...
随机推荐
- Emacs入门快捷键
打开emacs开始一个程序最基本操作: 1.打开Emacs,执行 $ emacs 2.建立一个新的程序文件. 按C-x C-f 然后在屏幕的底部出现minibuffer,光标提示你输入文件名称, 文件 ...
- HDU 1084 - ACM
题目不难,但是需要对数据进行处理,我的代码有些冗长,希望以后能改进... 主要思路是先算总的时间,然后进行对比,将做同样题数的前一半的人筛选出来. /状态:AC/ Description “Point ...
- [android]android开发中的运行错误之:adb.exe
调试的时候出现一下错误: The connection to adb is down, and a servera error has occured.You must restart adb and ...
- Object-c学习之路十(NSNumber&NSValue)
// // main.m // NSNumberAndNSValue // // Created by WildCat on 13-7-26. // Copyright (c) 2013年 wildc ...
- nopCommerce 3.3正式发布及新增功能改进
nopCommerce是一套优秀开源且基于Asp.net MVC的开源商城系统,nopCommerce 3.x经历长时间多个版本重构优化改进,目前已经趋于完善与成熟! nopCommerce 3.3正 ...
- hibernate update部分更新
hibernate update Hibernate 中如果直接使用 Session.update(Object o); 会把这个表中的所有字段更新一遍. 比如: view plaincopy to ...
- Node.js how to respond to an upgrade request?
You just need to call socket.write with the appropriate HTTP syntax as plain text along these lines ...
- webService设置超时时间
在客户端配置文件中设置: <bindings> <basicHttpBinding> <binding name="UrlCrawler ...
- ckeditor详解
源网页编辑软件FCKEditor在09年发布更新到3.0,并改名为CKEditor.改进后的ckeditor更加模块话,配置更加灵活,和以前的fckeditor使用方式上也有所不同.在我的mvc项目中 ...
- Mysql bigint 类型转为datetime
最近在使用quartz,在mysql中其数据库表中的时间都是使用bigint类型存储的,要想使其查询结果显示为yyyy-mm-dd hh:MM:ss的格式需要使用 from_unixtime()函数, ...