博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpClient 上传文件、下载文件
阅读量:6307 次
发布时间:2019-06-22

本文共 3738 字,大约阅读时间需要 12 分钟。

hot3.png

/** * 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.*;/** * 〈一句话功能简述〉
* 〈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 依赖:

org.apache.httpcomponents
httpclient
4.5.5
org.apache.httpcomponents
httpcore
4.4.9
org.apache.httpcomponents
httpmime
4.5.5

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

你可能感兴趣的文章
3星|《三联生活周刊》2017年39期:英国皇家助产士学会于2017年5月悄悄修改了政策,不再鼓励孕妇自然分娩了...
查看>>
linux查看命令是由哪个软件包提供的
查看>>
高级Linux工程师常用软件清单
查看>>
堆排序算法
查看>>
folders.cgi占用系统大量资源
查看>>
路由器ospf动态路由配置
查看>>
zabbix监控安装与配置
查看>>
python 异常
查看>>
last_insert_id()获取mysql最后一条记录ID
查看>>
可执行程序找不到lib库地址的处理方法
查看>>
bash数组
查看>>
Richard M. Stallman 给《自由开源软件本地化》写的前言
查看>>
oracle数据库密码过期报错
查看>>
修改mysql数据库的默认编码方式 .
查看>>
zip
查看>>
How to recover from root.sh on 11.2 Grid Infrastructure Failed
查看>>
rhel6下安装配置Squid过程
查看>>
《树莓派开发实战(第2版)》——1.1 选择树莓派型号
查看>>
在 Linux 下使用 fdisk 扩展分区容量
查看>>
结合AlphaGo算法和大数据的量化基本面分析法探讨
查看>>