前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >httpClient 发送压缩文件

httpClient 发送压缩文件

作者头像
shengjk1
发布2018-10-24 15:26:09
2.1K0
发布2018-10-24 15:26:09
举报
文章被收录于专栏:码字搬砖
代码语言:javascript
复制
    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();
                }
            }

        }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年10月17日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云服务器
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档