如何用httpclient 获取好友qq clientkeyhtml中的 pdf文件进行下载并保存在电脑 ,我做到获

java 已经httpclient获取pdf代码,如何把他pdf文件保存到本机,请前辈指点_百度知道HttpClient入门实例之简单的pdf文件爬虫
HttpClient入门实例之简单的pdf文件爬虫需求:从网址http://www3./wangshangketang/yuanneike/guanlixue/sjxz.htm上下载所有的pdf文件代码如下: import java.io.F import java.io.FileOutputS import java.io.IOE import java.io.InputS import java.net.URLE import java.util.ArrayL import java.util.L import java.util.T import java.util.TimerT
import org.apache.http.HttpE import org.apache.http.HttpR import org.apache.http.client.ClientProtocolE import org.apache.http.client.HttpC import org.apache.http.client.methods.HttpG import org.apache.http.conn.ClientConnectionM import org.apache.http.conn.params.ConnManagerP import org.apache.http.conn.params.ConnPerRouteB import org.apache.http.conn.scheme.PlainSocketF import org.apache.http.conn.scheme.S import org.apache.http.conn.scheme.SchemeR import org.apache.http.conn.ssl.SSLSocketF import org.apache.http.impl.client.DefaultHttpC import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnM import org.apache.http.params.BasicHttpP import org.apache.http.params.HttpConnectionP import org.apache.http.params.HttpP import org.apache.http.protocol.BasicHttpC import org.apache.http.protocol.HttpC
import org.htmlparser.N import org.htmlparser.NodeF import org.htmlparser.P import org.htmlparser.filters.AndF import org.htmlparser.filters.NodeClassF import org.htmlparser.tags.LinkT import org.htmlparser.util.NodeL import org.htmlparser.util.ParserE
public class Crawler implements Runnable{
public static String SAVE="C:/Users/Administrator/Downloads";//下载保存路径
private String url="";//要抓取的网页地址
public Crawler(String url){
public Crawler(){}
* @param url 要抓取的网页的地址
* @return 这个对应的内容
* @throws ClientProtocolException
* @throws IOException */
private String crawl(String url) throws ClientProtocolException, IOException{
System.out.println("[INFO] Crawl From : "+url);
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet=new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity=httpResponse.getEntity();
InputStream inStream=httpEntity.getContent();
String content="";
while(true){
byte[] bytes=new byte[];
int k=inStream.read(bytes);
if(k&=0)content=content+new String(bytes,0,k);
System.out.println(content);
System.out.println("=========================================================================================");
public void run(){
String prefix=this.url.substring(0,this.url.lastIndexOf("/"));
String content=this.crawl(this.url);//抓取网页内容
Parser parser=new Parser(content); //使用HTMLParser对网页内容进行解析
filter=new NodeClassFilter(LinkTag.class);
filter=new AndFilter(filter,new NodeFilter(){
public boolean accept(Node node) {
return ((LinkTag)node).isHTTPLink();
list=parser.extractAllNodesThatMatch(filter);
List&String& urlsList =new ArrayList&String&();
for(int i=0;i&list.size();i++){
String[] array=list.elementAt(i).getText().split("/"");
if(array[1].endsWith(".pdf")||array[1].endsWith(".PDF")){//只下载pdf
String downloadUrl=new String(prefix+"/"+array[1]);
urlsList.add(downloadUrl);//生成需要下载的地址
//从这里开始是进行下载,使用了多线程执行请求
HttpParams params=new BasicHttpParams();
//ConnManagerParams.setTimeout(params, 60000*3); //设置连接最大等待时间
ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(50));//设置并发数 //HttpConnectionParams.setConnectionTimeout(params, 60000*2);
//设置连接超时时间
HttpConnectionParams.setSoTimeout(params, 60000*10);//设置读取超时时间
SchemeRegistry schemeRegistry=new SchemeRegistry();
schemeRegistry.register(new Scheme("http",PlainSocketFactory.getSocketFactory(),80));
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ThreadSafeClientConnManager cm=new ThreadSafeClientConnManager(params,schemeRegistry);
HttpClient httpClient=new DefaultHttpClient(cm,params);
Thread[] threads=new Thread[urlsList.size()];
for(String url:urlsList){
String path=Crawler.SAVE+url.substring(url.lastIndexOf("/"), url.length());
url=url.substring(0, url.lastIndexOf("/"))+"/"+URLEncoder.encode(url.substring(url.lastIndexOf("/")+1,url.length()),"UTF-8");
HttpGet httpGet=new HttpGet(url);
threads[n]=new Thread(new Downloader(httpClient,httpGet,url,path));
for(Thread thread:threads)thread.start();
for(Thread thread:threads)if(thread.isAlive())thread.join();
}catch (InterruptedException e) {
System.out.println("[ERROR] Download InterruptedException : "+e.toString());
//e.printStackTrace();
} catch (ParserException e) {
System.out.println("[ERROR] Parse ParserException : "+e.toString());
//e.printStackTrace();
}catch (ClientProtocolException e) {
System.out.println("[ERROR] Crawl ClientProtocolException : "+e.toString());
//e.printStackTrace();
} catch (IOException e) {
System.out.println("[ERROR] Crawl IOException : "+e.toString());
//e.printStackTrace();
public static void main(String[] args) {
//入口程序
Crawler crawler=new Crawler("http://www3./wangshangketang/yuanneike/guanlixue/sjxz.htm");//这里设定网页地址
Thread thread=new Thread(crawler);
thread.start();
//类Downloader真正的执行了写入网络数据到文件的步骤 class Downloader implements Runnable{
private String url="";
private String path="";
private final HttpClient httpC
private final HttpContext httpC
private final HttpGet httpG
* @param httpClient 多个线程共享的HtppClient
* @param httpGet 要下载的HttpGet
* @param url 资源网络地址
* @param path 资源下载之后本地的保存路径 */
public Downloader(HttpClient httpClient,HttpGet httpGet,String url,String path){
this.httpClient=httpC
this.httpGet=httpG
this.httpContext=new BasicHttpContext();
this.path=
public void run() {
System.out.println("[INFO] Download From : "+this.url);
File file=new File(this.path);
if(file.exists())file.delete();
//使用file来写入本地数据
file.createNewFile();
FileOutputStream outStream = new FileOutputStream(this.path);
//执行请求,获得响应
HttpResponse httpResponse = this.httpClient.execute(this.httpGet,this.httpContext);
System.out.println("[STATUS] Download : "+httpResponse.getStatusLine()+" [FROM] "+this.path);
HttpEntity httpEntity=httpResponse.getEntity();
InputStream inStream=httpEntity.getContent();
while(true){//这个循环读取网络数据,写入本地文件
byte[] bytes=new byte[];
int k=inStream.read(bytes);
outStream.write(bytes,0,k);
outStream.flush();
inStream.close();
outStream.close();
} catch (IOException e){
this.httpGet.abort();
System.out.println("[ERROR] Download IOException : "+e.toString()+" [FROM] : "+this.path);
//e.printStackTrace();
最新教程周点击榜
微信扫一扫httpClient怎么获取网页中js执行完后的网页源码_百度知道使用httpclient分析一个网站上的数据,网站使用了异步传输,得到的结果页面的HTML源文件没有数据,需要如何获得?
[问题点数:40分,结帖人quechao123]
使用httpclient分析一个网站上的数据,网站使用了异步传输,得到的结果页面的HTML源文件没有数据,需要如何获得?
[问题点数:40分,结帖人quechao123]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2011年4月 Java大版内专家分月排行榜第二2010年8月 Java大版内专家分月排行榜第二2010年5月 Java大版内专家分月排行榜第二2008年2月 Java大版内专家分月排行榜第二2007年7月 Java大版内专家分月排行榜第二
2011年2月 Java大版内专家分月排行榜第三2010年9月 Java大版内专家分月排行榜第三2008年9月 Java大版内专家分月排行榜第三2008年1月 Java大版内专家分月排行榜第三2007年11月 Java大版内专家分月排行榜第三2007年9月 Java大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&

我要回帖

更多关于 获取qq clientkey 的文章

 

随机推荐