使用httpclient下载 页面图片

Posted zhao1949

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用httpclient下载 页面图片相关的知识,希望对你有一定的参考价值。

代码

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.CookieSpecs;
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;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyHttpClientUtils {
    
    private static Logger logger = LoggerFactory.getLogger(MyHttpClientUtils.class);
    
    private static final int  HTTPCLIENT_TIMEOUT = 5000;

    public static Tuple2<Boolean,String> getPageByUrl(String pageUrl, String encode) throws UnsupportedEncodingException{
        logger.info("pageurl=" + pageUrl);
        String body = null;
        RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(HTTPCLIENT_TIMEOUT)   //socket超时
                    .setConnectTimeout(HTTPCLIENT_TIMEOUT)   //connect超时
                    .build();
        CloseableHttpClient httpClient = HttpClients.custom()
                    .setDefaultRequestConfig(requestConfig)
                    .build();
            HttpGet httpGet = new HttpGet(pageUrl);
            try {
                CloseableHttpResponse response = httpClient.execute(httpGet);           
                String statusCode = String.valueOf(response.getStatusLine().getStatusCode());
                logger.info("getStatusCode=" + response.getStatusLine().getStatusCode());
                if(response.getStatusLine().getStatusCode() != 200) {
                    logger.info("返回码异常:" + response.getStatusLine().getStatusCode());
                    return new Tuple2<Boolean, String>(false, null);
                }
                body = EntityUtils.toString(response.getEntity(), encode);
//                System.out.println("body=" + body);
            } catch (IOException e) {
                System.out.println("----------Connection timeout--------");
//                return ne
            }
            return new Tuple2<Boolean, String>(true, body);
    }
    
    
    public static Tuple2<Boolean,byte[]> getPicByteArray(String picUrl) throws ClientProtocolException, IOException {
        logger.info("下载url=" + picUrl);
        RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setConnectionRequestTimeout(HTTPCLIENT_TIMEOUT).setConnectTimeout(HTTPCLIENT_TIMEOUT).build();
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig).build();
        HttpGet httpGet = new HttpGet(picUrl);
        CloseableHttpResponse response = httpClient.execute(httpGet);

        logger.info("返回的状态码:" + response.getStatusLine().getStatusCode());
        if(response.getStatusLine().getStatusCode() == 200) {
            byte[] bytes = EntityUtils.toByteArray(response.getEntity());
            return new Tuple2<>(true, bytes);
        }else{
            return new Tuple2<>(false, null);
        }
    }
}
package testGetpic;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.client.config.CookieSpecs;
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;
import org.apache.http.util.EntityUtils;

public class TestPic2 {

    public static void main(String[] args) throws IOException{
        RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setConnectionRequestTimeout(6000).setConnectTimeout(6000).build();
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig).build();
        HttpGet httpGet = new HttpGet("http://xxxx.com/abc.jpg");
        CloseableHttpResponse response = httpClient.execute(httpGet);
        Header[] headerArray =  response.getAllHeaders();
        for(Header h : headerArray) {
            System.out.println(h.getName());
            System.out.println(h.getValue());
            System.out.println("======");
        }
        
        System.out.println("---------------");
        
        System.out.println(response.getStatusLine().getStatusCode());
        if(response.getStatusLine().getStatusCode() == 200) {
            
//            for(Header h :response.getAllHeaders()){
//                System.out.println(h.getElements().length);
//                for(HeaderElement he :h.getElements()){
//                    System.out.println("pc="  + he.getParameterCount());
//                }
//                System.out.println( h.getName() );
//                System.out.println( h.getValue() );
//            }
            
            byte[] b = EntityUtils.toByteArray(response.getEntity());
            FileOutputStream fos = new FileOutputStream("test2.jpg");
            fos.write(b);
            fos.close();
        }
        
    }
    
}

 

以上是关于使用httpclient下载 页面图片的主要内容,如果未能解决你的问题,请参考以下文章

安卓小案例收集二

HttpClient请求服务器图片

根据图片的url地址下载图片到本地保存代码片段

小程序各种功能代码片段整理---持续更新

Android studio 中想用 HttpClient 下载网络图片,却没有 HttpClient 以及相关的类

如何在 C# HttpClient 中循环调用分页 URL 以从 JSON 结果中下载所有页面