POST/GET从远程服务器下载文件

😂 这篇文章最后更新于754天前,您需要注意相关的内容是否还可用。

封装的工具类,get下载调用downloadURL(url),post下载调用POSTMethod()

timg (1).jpg

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.io.ByteArrayOutputStream;
import java.util.*;

/**
 * User: Cheng
 * Date: 2019/4/22
 * Time: 17:33
 * Description: No Description
 */
public class PostUtil {
    /**
     * 以Post方法访问
     *
     * @param url     请求地址
     * @param argsMap 携带的参数
     * @param content 内容
     * @return String 返回结果
     * @throws Exception
     */
    public static byte[] POSTMethod(String url, Map<String, Object> argsMap, String content) throws Exception {
//        argsMap,content都可以带参,二选一
        byte[] dataByte = null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        if (MapUtils.isNotEmpty(argsMap)) {
            //设置参数
            UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(setHttpParams(argsMap), "UTF-8");
            httpPost.setEntity(encodedFormEntity);
        }
        if (StringUtils.isNotEmpty(content)) {
            httpPost.setEntity(new ByteArrayEntity(content.getBytes()));
        }
        // 执行请求
        HttpResponse httpResponse = httpClient.execute(httpPost);
        // 获取返回的数据
        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null) {
            byte[] responseBytes = getData(httpEntity);
            dataByte = responseBytes;
            httpPost.abort();
        }
        //将字节数组转换成为字符串
//        String result = bytesToString(dataByte);
//        return result;
        return dataByte;
    }

    /**
     * 获取Entity中数据
     *
     * @param httpEntity
     * @return
     * @throws Exception
     */
    public static byte[] getData(HttpEntity httpEntity) throws Exception {
        BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bufferedHttpEntity.writeTo(byteArrayOutputStream);
        byte[] responseBytes = byteArrayOutputStream.toByteArray();
        return responseBytes;
    }

    /**
     * 设置HttpPost请求参数
     * @param argsMap
     * @return BasicHttpParams
     */
    private static List<BasicNameValuePair> setHttpParams(Map<String, Object> argsMap){
        List<BasicNameValuePair> nameValuePairList = new ArrayList<BasicNameValuePair>();
        //设置请求参数
        if (argsMap!=null && !argsMap.isEmpty()) {
            Set<Map.Entry<String, Object>> set = argsMap.entrySet();
            Iterator<Map.Entry<String, Object>> iterator = set.iterator();
            while(iterator.hasNext()){
                Map.Entry<String, Object> entry = iterator.next();
                BasicNameValuePair basicNameValuePair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
                nameValuePairList.add(basicNameValuePair);
            }
        }
        return nameValuePairList;
    }

    public static byte[] downloadURL(String url) throws Exception {
        byte[] dataByte = null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();//获取返回数据
        if (entity != null) {
//            根据entity创建BufferedHttpEntity与创建流对象
            BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(entity);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//          将BufferedHttpEntity写到流中
            bufferedHttpEntity.writeTo(byteArrayOutputStream);
//            将流转换成byte[]
            byte[] responseBytes = byteArrayOutputStream.toByteArray();
            dataByte = responseBytes;
            httpGet.abort();    //(终止)程序
        }
        return dataByte;
    }
}