|
参考资料(1)Apache HttpClients的类库下载地址:http://hc.apache.org/downloads.cgi
(2)Apache HttpClients的API 的JavaDoc文档:http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/index.html
(3)本文参考了笔者所写的《Java网络编程核心技术详解》,电子工业出版社出版。
![点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小](png/topadv-javanet.png)
(4)更多抓取网页数据的各种实用技巧,请参考:盘点用Java抓取HTTP服务器和FTP服务器的网页数据或图片等数据的实用技巧
下载图片的范例import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
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;
/* 利用Apache的HttpClient来下载图片等二进制文件非常方便
但是它没有执行JavaScript脚本功能,因此不能正确获得需要执行JavaScript脚本才能产生的HMTL文档
*/
public class ApacheClient {
public static void main(String[] args) throws IOException{
byte[] imgData=getByteSource("http://www.javathinker.net/image/topadv-funnyjava.png");
FileOutputStream out=new FileOutputStream("C:\tmp\\topadv-funnyjava.png");
out.write(imgData); //把图片数据保存到文件中
out.close();
}
/** 获取参数url对应的数据的字节流 */
public static byte[] getByteSource(String url) throws IOException{
CloseableHttpClient client = null;
byte[] result=null;
client = HttpClients.createDefault();
result=getData(client,url);
client.close();
return result;
}
/** 通过 Apache CloseableHttpClient来读取数据 */
public static byte[] getData(CloseableHttpClient client, String url)throws IOException {
ByteArrayOutputStream buffer=null;
// 发送get请求
HttpGet request = new HttpGet(url);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(50000).setConnectTimeout(50000).build();
// 设置请求头
request.setHeader("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1");
request.setConfig(requestConfig);
CloseableHttpResponse response = client.execute(request);
if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
buffer=new ByteArrayOutputStream();
byte[] buff=new byte[1024];
int len=-1;
while((len=in.read(buff))!=-1){
buffer.write(buff,0,len);
}
}
request.releaseConnection();
if(buffer!=null)
return buffer.toByteArray();
else
return null;
}
} |
作者:孙卫琴
程序猿的技术大观园:www.javathinker.net
[这个贴子最后由 admin 在 2021-10-09 10:52:02 重新编辑]
|
网站系统异常
系统异常信息 |
Request URL:
http://www.javathinker.net/WEB-INF/lybbs/jsp/topic.jsp?postID=1392
java.lang.NullPointerException
如果你不知道错误发生的原因,请把上面完整的信息提交给本站管理人员。
|
|