>>分享孙卫琴的Java技术专稿和著作 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 22745 个阅读者 刷新本主题
 * 贴子主题:  【Java网络编程专题】用Apache HttpClients下载网上的图片等各种数据 回复文章 点赞(0)  收藏  
作者:sunweiqin    发表时间:2020-01-10 11:24:15     消息  查看  搜索  好友  邮件  复制  引用

参考资料

(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+鼠标滚轮放大或缩小
(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

如果你不知道错误发生的原因,请把上面完整的信息提交给本站管理人员