专栏首页Jerry的SAP技术分享一段pdf转word的Java代码

一段pdf转word的Java代码

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://jerry.blog.csdn.net/article/details/101169239

import java.io.BufferedReader;  
import java.io.DataOutputStream;
import java.io.IOException;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
	  
import org.apache.http.HttpEntity;  
import org.apache.http.HttpResponse;  
import org.apache.http.client.ClientProtocolException;  
import org.apache.http.client.HttpClient;  
import org.apache.http.client.methods.HttpGet;  
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient; 
	
public class pdf2word 
{
	final private static String GET_PREFIX = "http://online2pdf.com/progress_frame?upload_id=";
	final private static String POST_PREFIX = "http://online2pdf.com/conversion/frame";
	private static UUID upload_guid;
	private static String convertStreamToString(InputStream is) {      
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));      
        StringBuilder sb = new StringBuilder();      
        String line = null;      
        try {      
            while ((line = reader.readLine()) != null) {  
                sb.append(line + "\n");      
            }      
        } catch (IOException e) {      
            e.printStackTrace();      
        } finally {      
            try {      
                is.close();      
            } catch (IOException e) {      
               e.printStackTrace();      
            }      
        }      
        return sb.toString();      
    }  
	
	private static void sendHTTPPostRequest()
	{

        Map<String,Object> params = new LinkedHashMap<>();
        params.put("UPLOAD_ID", upload_guid);
        params.put("MAX_FILE_SIZE", "52428800");
        params.put("file_order", "0_0;1_0;");
        params.put("userfile[0][]", "File Name=\"1.pdf\"");
        params.put("conversion_mode", "merge");
        params.put("convert", "1");
        params.put("orientation", "auto");
        params.put("merge_mode", "serial");
        params.put("output_format", "pdf");
        params.put("page_size", "a4");
        params.put("quality", "80");

        
        try {
        	StringBuilder postData = new StringBuilder();
            for (Map.Entry<String,Object> param : params.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");

    		URL url = null;
    		try {
    			url = new URL("http://online2pdf.com/conversion/frame");
    		} catch (MalformedURLException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "multipart/form-data");
            conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
            conn.setDoOutput(true);
			conn.getOutputStream().write(postDataBytes);
			Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
	        for (int c; (c = in.read()) >= 0; System.out.print((char)c));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void sendHTTPGetRequest()
	{
		upload_guid = java.util.UUID.randomUUID();
		String getURL = GET_PREFIX + upload_guid;
		HttpGet httpgets = new HttpGet(getURL);    
		HttpClient httpclient = new DefaultHttpClient();  
        HttpResponse response;
		try {
			response = httpclient.execute(httpgets);
			HttpEntity entity = response.getEntity();    
	        if (entity != null) 
	        {    
	            InputStream instreams = entity.getContent();    
	            String str = convertStreamToString(instreams);
	            System.out.println(str);
	        }
		} catch ( IOException e) {
			e.printStackTrace();
		}    
	}
	
	public static void main(String[] args) throws ClientProtocolException, IOException  
	{  
		sendHTTPGetRequest();
		sendHTTPPostRequest();
	}  
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 如何让SAP Spartacus的非英语页面正常显示

    如下图所示,当修改Spartacus的语言成德语时,我们在界面上看到很多用大括号包裹起来的占位符,这些占位符是Spartacus internationaliz...

    Jerry Wang
  • SAP Spartacus ProductOccModule

    这个defaultOccProductConfig主要是endpoint config:

    Jerry Wang
  • 使用SAP云平台 + JNDI访问Internet Service

    以Internet Service http://maps.googleapis.com/maps/api/distancematrix/xml?origins...

    Jerry Wang
  • 【微信开发】 红包接口开发

    参考网上好几个版本的答案咯~ 分装 红包工具类 : package com.tepusoft.web.weixin.utils; import java.io...

    冷冷
  • SpringBoot之API--Swagger2接口文档管理

    本地的访问地址: http://localhost:9091/swagger-ui.html

    MiChong
  • LGBT主题式的VR体验,让你感知同志们的辛酸苦楚

    VRPinea
  • 仓库ERP管理系统(springboot)

    基于SpringBoot框架和SaaS模式,非常好用的ERP软件,目前专注进销存+财务功能。主要模块有零售管理、采购管理、销售管理、仓库管理、财务管理、报表查询...

    C you again 的博客
  • Django 2.1.7 模板 - 动态配置静态文件路径

    Django 2.1.7 创建应用模板 Django 2.1.7 配置公共静态文件、公共模板路径 Django 2.1.7 模板语言 - 变量、标签、过滤器...

    Devops海洋的渔夫
  • AI 医疗三类证终于出了!科亚医疗“冠脉血流储备分数计算软件”获批上市

    雷锋网AI掘金志消息,近日,国家药品监督管理局经审查,批准了北京昆仑医云科技有限公司(科亚医疗)生产的创新产品“冠脉血流储备分数计算软件”的注册。这是首个以“深...

    AI掘金志
  • springBoot初探-创建项目

    一边学习公司用到的技术,一边重构小程序后端,从而更好的理解公司搭建的框架。此处记录一个用idea+gradle+springboot的基础实现。

    汐楓

扫码关注云+社区

领取腾讯云代金券