All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qingxun.javasdkapi.examples.DocConvertOpenApiClientDemo Maven / Gradle / Ivy

Go to download

openapi SDK for Java Copyright © 2023 杭州轻寻科技 All rights reserved. 版权所有 (C)杭州轻寻科技 http://open.qingxun.com

The newest version!
package com.qingxun.javasdkapi.examples;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qingxun.javasdkapi.clients.DocConvertOpenApiClient;
import com.qingxun.javasdkapi.request.ConvertRequest;
import com.qingxun.javasdkapi.request.DownloadFileRequest;
import com.qingxun.javasdkapi.request.QueryTransProgressRequest;
import com.qingxun.javasdkapi.response.ConvertResponse;
import com.qingxun.javasdkapi.response.DownloadFileResponse;
import com.qingxun.javasdkapi.response.QueryTransProgressResponse;
import com.qingxun.javasdkapi.utils.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import static com.qingxun.javasdkapi.common.JsonUtil.objectMapper;

/**
 * 格式转换
 */
public class DocConvertOpenApiClientDemo extends CommonDemo {

    private final static String appId = "您的appId";
    private final static String privateKey = "您的privateKey";
    private final static String convertFilePath = "需要格式转换的文件";
    private final static String saveConvertedDirPath = "保存转换后的文件目录名称";
    private final static String saveConvertedFileName = "保存转换后的文件名称";
    //转换的目标格式 -1:word;-3:excel;-6:pptx,-8:pdf,-9:ofd。
    private final static Integer conversionformat = -8;
    //文件名称,传入文件流的时候必须传
    private final static String docName = "你的文件全名名称";
    /**
     * 下载的文件类型(new):
     * 2:翻译后的pdf文件,支持格式:word、pdf、img。
     * 3:翻译后的word文件(支持格式:word、pdf);ppt文件(支持格式:img、ppt);excel文件(支持格式:excel)
     * 6:转换完成文档
     */
    private final static Integer dType =6;
    //源文件的语言(扫描件需设置语言)
    private final static  String fromLan="zh";

    private final static Logger log = LoggerFactory.getLogger(DocConvertOpenApiClientDemo.class);

    private static DocConvertOpenApiClient docConvertOpenApiClient = new DocConvertOpenApiClient(appId, privateKey);

    //可以自定义请求接口的工具类使用更符合自己生产环境或者更高效的工具类,目前只支持传入文件流(FileStream)的方式而不是传入文件(file)的方式
    //private static DocConvertOpenApiClient docConvertOpenApiClient = new DocConvertOpenApiClient(appId, privateKey,new HttpClientUtil());

    public static ConvertResponse convertFile(String filePath) {
        ConvertRequest convertRequest = null;
        try {
            convertRequest = new ConvertRequest.Builder()
                    .setFile(new File(filePath))
                    //传入文件/文件流都可
                    //.setFileStream(new FileInputStream(filePath)).setFileName(docName)
                    .setFrom(fromLan)
                    .setConversionFormat(conversionformat)
                    .builder();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return docConvertOpenApiClient.excute(convertRequest);
    }

    public static QueryTransProgressResponse queryTransProcess(Integer tid) {
        QueryTransProgressRequest queryTransProgressRequest = new QueryTransProgressRequest.Builder().setTid(tid).builder();
        return docConvertOpenApiClient.excute(queryTransProgressRequest);
    }

    public static DownloadFileResponse downloadFile(Integer tid, Integer dtype) {
        DownloadFileRequest downloadFileRequest = new DownloadFileRequest.Builder().setTid(tid).setDtype(dtype).builder();
        return docConvertOpenApiClient.excute(downloadFileRequest);

    }


    public static void main(String[] args) throws JsonProcessingException {
        ConvertResponse convertResponse = convertFile(convertFilePath);
        log.info("转换文件结果{}", objectMapper.writeValueAsString(convertResponse));
        QueryTransProgressResponse queryTransProgressResponse;
        boolean flag = true;
        while (flag) {
            //查询格式转换后的转换进度
            queryTransProgressResponse = queryTransProcess(convertResponse.getTid());
            log.info("查询转换文件进度结果{}", objectMapper.writeValueAsString(queryTransProgressResponse));
            if (queryTransProgressResponse == null) {
                return;
            }
            if (queryTransProgressResponse.getStatus().equals(314)) {
                //下载格式转换后的文件
                DownloadFileResponse response = downloadFile(convertResponse.getTid(), dType);
                //保存格式转换后得到的文件
                bytesToFile(response.getBytes(), saveConvertedDirPath, saveConvertedFileName + response.getDocType());
                return;
            }
            if (queryTransProgressResponse.getStatus().equals(313)) {
                flag = true;
            } else {
                flag = false;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy