• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
漫椿
博客园    首页    新随笔    联系   管理    订阅  订阅
HttpClient工具类
 
package com.atguigu.springannotation.util;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

/**
 * HttpClient工具类
 *
 * @author Administrator
 */
@Component
public class HttpUtils {

    //创建HttpClient管理器
    private PoolingHttpClientConnectionManager cm;

    public HttpUtils() {
        this.cm = new PoolingHttpClientConnectionManager();

        //设置最大连接数
        this.cm.setMaxTotal(100);

        //设置主机连接数
        this.cm.setDefaultMaxPerRoute(10);

    }

    /**
     * 根据请求地址下载页面数据
     *
     * @param url
     * @return 页面的数据
     */
    public String doGetHt(String url) {
        //获取·HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //创建httpGet请求对象,设置url地址
        HttpGet httpGet = new HttpGet(url);

        //设置请求信息
        httpGet.setConfig(this.getConfig());

        CloseableHttpResponse response = null;

        try {
            //使用HttpClient发起请求,获取响应
            response = httpClient.execute(httpGet);
            // 解析响应,返回结果
            if (response.getStatusLine().getStatusCode() == 200) {
                //判断响应体是否为空,若不为空可以使用EntityUtils
                if (response.getEntity() != null) {
                    String content = EntityUtils.toString(response.getEntity(), "utf8");
                    return content;

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭response
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
        return "";

    }


    /**
     * 下载图片
     *
     * @param url
     * @return 图片名称
     */
    public String doGetImage(String url) {
        //获取·HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //创建httpGet请求对象,设置url地址
        HttpGet httpGet = new HttpGet(url);

        //设置请求信息
        httpGet.setConfig(this.getConfig());

        CloseableHttpResponse response = null;

        try {
            //使用HttpClient发起请求,获取响应
            response = httpClient.execute(httpGet);
            // 解析响应,返回结果
            if (response.getStatusLine().getStatusCode() == 200) {
                //判断响应体是否为空,若不为空可以使用EntityUtils
                if (response.getEntity() != null) {
                    //下载图片
                    //获取图片的后缀
                    String extName = url.substring(url.lastIndexOf("."));

                    //创建图片名,重命名图片
                    String picName = UUID.randomUUID().toString() + extName;

                    //下载图片
                    //声明OutPutStream
                    OutputStream outputStream = new FileOutputStream(new File("E:\\images" + picName));
                    response.getEntity().writeTo(outputStream);

                    //返回图片名称
                    return picName;


                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭response
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
        return "";

    }

    /**
     * 设置请求信息
     *
     * @return
     */
    private RequestConfig getConfig() {
        RequestConfig config = RequestConfig
                .custom()
                .setConnectTimeout(1000)  //创建链接的最长时间,单位是毫秒
                .setConnectionRequestTimeout(500)   //设置获取链接最长时间
                .setSocketTimeout(10 * 1000)          //设置数据传输的最长时间
                .build();
        return config;
    }


}

 

posted on 2022-07-27 16:45  编程耽误的厨子  阅读(207)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3