发报文工具java_httpclient工具类代码 发送报文和文件

package com.ygp.wms.modular.exportdistributionnotice.util; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.PartSource; import org.apache.commons.httpclient.methods.multipart.StringPart; import org.apache.commons.httpclient.params.HttpClientParams; import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.ByteArrayBody; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; /** * @ClassName: HttpUtils.java * @Description: http工具类 * @author: guwx wenxiang.gu@xwwwx.com * @version: V1.0 * @Date: 2019年3月13日 上午11:37:20 */ public class HttpUtils { private static final String CHARSET = "UTF-8"; /** 连接超时 */ private static final int CONNECT_TIMEOUT = 60 * 1000; /** 请求超时 */ private static final int SO_TIMEOUT = 60 * 1000; /** * @Title: sendFile * @Description: 通过http发送文件和参数 * @param params * @param url * @param connectTimeout * @param socketTimeout * @return * @throws Exception * @author: guwx wenxiang.gu@xwwwx.com * @date: 2019年3月13日 上午11:37:47 * @version: V1.0 * @throws */ public static String sendFile(Map params, String url) throws Exception { PostMethod post = new PostMethod(url); MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); Part[] parts = new Part[params.size()]; try { int i = 0; for (Entry entry : params.entrySet()) { if (entry.getValue() instanceof File) { parts[i] = new FilePart(entry.getKey(),(File) entry.getValue()); } else { parts[i] = new StringPart(entry.getKey(), entry.getValue().toString()); } i++; } } catch (FileNotFoundException e) { throw new Exception( "待发送文件不存在!", e); } post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams())); doExecute(post,CONNECT_TIMEOUT,SO_TIMEOUT); String respStr = null; try { respStr = post.getResponseBodyAsString(); } catch (IOException e) { throw new Exception( "从Response读取取返回数据时发生异常!", e); } return respStr; } /** * @Title: doExecute * @Description: http通讯 * @param post * @param connectTimeout * @param socketTimeout * @throws Exception * @author: guwx wenxiang.gu@xwwwx.com * @date: 2019年3月13日 上午11:38:32 * @version: V1.0 * @throws */ private static void doExecute(HttpMethod post,int connectTimeout,int socketTimeout) throws Exception { HttpClient httpClient = new HttpClient(); try { HttpClientParams hcp = new HttpClientParams(); hcp.setSoTimeout(socketTimeout); hcp.setConnectionManagerTimeout(connectTimeout); hcp.setContentCharset(CHARSET); httpClient.setParams(hcp); httpClient.executeMethod(post); } catch (HttpException e) { throw new Exception("网络连接发生异常!", e); } catch (IOException e) { throw new Exception( "发送消息发生异常!", e); } } public static String postFileMultiPart(String url, Map reqParam) throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 创建httpget. HttpPost httppost = new HttpPost(url); RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT) .setSocketTimeout(SO_TIMEOUT).build(); httppost.setConfig(defaultRequestConfig); MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); for (Entry param : reqParam.entrySet()) { if (param.getValue() instanceof File) { byte[] bytes = readBytesFromFile((File) param.getValue()); multipartEntityBuilder.addBinaryBody(param.getKey(), bytes); }else { multipartEntityBuilder.addTextBody(param.getKey(),param.getValue().toString()); } } HttpEntity reqEntity = multipartEntityBuilder.build(); httppost.setEntity(reqEntity); // 执行post请求. CloseableHttpResponse response = httpclient.execute(httppost); try { // 获取响应实体 HttpEntity entity = response.getEntity(); if (entity != null) { return EntityUtils.toString(entity, CHARSET); } } finally { response.close(); } } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } private static byte[] readBytesFromFile(File file) { FileInputStream fileInputStream = null; byte[] bytesArray = null; try { bytesArray = new byte[(int) file.length()]; //read file into bytes[] fileInputStream = new FileInputStream(file); fileInputStream.read(bytesArray); } catch (IOException e) { e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return bytesArray; } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值