httpClient 发送压缩文件

    import org.apache.commons.codec.digest.DigestUtils;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.lang.StringUtils;
    import org.apache.http.HttpEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpUriRequest;
    import org.apache.http.client.methods.RequestBuilder;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    import org.apache.http.entity.mime.content.FileBody;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    import org.apache.log4j.Logger;
    import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;

    /**
     * Created by shengjk1  on 2016/6/23.
     */
    public class FileTransferUtil11 {
        private final static Logger logger = Logger.getLogger(FileTransferUtil11.class);

        public static void FileTransfer(String fileZipName, String ServiceAddr, String filename, String md5Hex) throws IOException {
            CloseableHttpResponse httpResponse = null;
            CloseableHttpClient httpClient = null;

            HttpUriRequest httpUriRequest=null;
            try {
                long currentTime = System.currentTimeMillis();

                httpClient = HttpClientBuilder.create().setRetryHandler(new DefaultHttpRequestRetryHandler()).build();///默认失败后重发3次,也可以自定义


                HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
                factory.setConnectTimeout(1000*60*3); //单位毫秒 2min
                factory.setReadTimeout(1000*60*5);

                File f = new File(fileZipName);
                FileBody fileBody = new FileBody(f);
                HttpEntity fileEntity = MultipartEntityBuilder.create().addPart(f.getName(), fileBody).build();

                httpUriRequest = RequestBuilder.post(ServiceAddr).addParameter("md5Hex", md5Hex).addParameter("filename", filename).setEntity(fileEntity).build();



                httpResponse = httpClient.execute(httpUriRequest);
                System.out.println("执行发送");


                long end = System.currentTimeMillis();
                long timeConsuming = (end - currentTime) / 1000;
                System.out.println("文件传输耗时:" + timeConsuming + "秒");

                int statusCode = httpResponse.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_OK) {

                    System.out.println("文件传输服务器正常响应!");

                    HttpEntity entity = httpResponse.getEntity();
                    String response = EntityUtils.toString(entity);

                    System.out.println("返回结果:" + response);
                    if (StringUtils.isNotEmpty(response)) {
                        if ("ack".equals(response)) {
                            System.out.println("数据发送成功!");
                        } else if ("md5_error".equals(response)) {
                            throw new RuntimeException("发送失败,md5_error!");
                        } else {
                            throw new RuntimeException("文件发送失败,返回结果为"+response);
                        }
                    }
                    EntityUtils.consume(entity);
                } else {
                    throw new RuntimeException("http异常状态码 :" + statusCode);
                }

            } catch (Exception e) {
                throw new RuntimeException("发送zip时 "+e);
            } finally {

                if (httpClient != null) {
                    httpClient.close();
                }
                if(httpResponse!=null){
                    httpResponse.close();
                }
            }

        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shengjk1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值