HttpClient 上传文件、下载文件

/**
 * FileName: PDFToDOCXUtil
 * Author:   xiangjunzhong
 * Date:     2018/3/6 14:17
 * Description: PDF转DOCX工具类
 */
package com.gibbons.commonserver.util;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
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.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.CharsetUtils;
import org.apache.http.util.EntityUtils;

import java.io.*;

/**
 * 〈一句话功能简述〉<br>
 * 〈PDF转DOCX工具类〉
 *
 * @author xiangjunzhong
 * @create 2018/3/6 14:17
 * @since 1.0.0
 */
public class PDFToDOCXUtil {

    private static CloseableHttpClient httpClient;

    private static final String URL = "XXXXXXXXXXXXXXXXXXXXXXXXXXX";

    static {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(100);
        cm.setDefaultMaxPerRoute(20);
        cm.setDefaultMaxPerRoute(50);
        httpClient = HttpClients.custom().setConnectionManager(cm).build();
    }

    /**
     * PDF TO DOCX
     *
     * @throws ParseException
     * @throws IOException
     */
    public static void postFile(String path) throws ParseException, IOException {
        try {
            String filePath = new String(path);
            HttpPost httpPost = new HttpPost(URL);
            File file = new File(filePath);
            FileBody bin = new FileBody(file);
            HttpEntity reqEntity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                    .addPart("data", bin)
                    .setCharset(CharsetUtils.get("UTF-8")).build();
            httpPost.setEntity(reqEntity);

            // 发起请求 并返回请求的响应
            CloseableHttpResponse response = httpClient.execute(httpPost);
            try {
                // 打印响应状态
                System.out.println("----------------------------------------:" + response.getStatusLine());
                // 获取响应对象
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    // 打印响应长度
                    System.out.println("Response content length: " + resEntity.getContentLength());
                    inputStreamToFile(resEntity.getContent(), new File(filePath.substring(0, filePath.lastIndexOf(".")) + ".docx"));
                }
                // 销毁
                EntityUtils.consume(resEntity);
            } finally {
                response.close();
            }
        } finally {
            httpClient.close();
        }
    }

    public static void inputStreamToFile(InputStream ins, File file) throws IOException {
        OutputStream os = new FileOutputStream(file);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();
    }

    public static void main(String[] args) {
        try {
            postFile("C:\\Users\\Administrator\\Desktop\\合同111.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Maven 依赖:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.5</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.4.9</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.5</version>
</dependency>

转载于:https://my.oschina.net/gibbons/blog/1630446

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值