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

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

package com.qingxun.javasdkapi.examples;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qingxun.javasdkapi.clients.DocTransOpenApiClient;
import com.qingxun.javasdkapi.request.*;
import com.qingxun.javasdkapi.response.*;
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 DocTransOpenApiClientDemo extends CommonDemo {


    private final static String uploadFilePath = "需要上传的文件路径";
    private final static String appId = "您的appId";
    private final static String privateKey = "您的privateKey";
    private final static String fromLan = "文件的原语言";
    private final static String toLan = "文件的目标语言";
    private final static String saveTransDirPath = "保存翻译后的文件目录名称";
    private final static String saveTransFileName = "保存翻译后的文件名称";
    //文件名称,传入文件流的时候必须传
    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 = 3;

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

    private static DocTransOpenApiClient docTransOpenApiClient = new DocTransOpenApiClient(appId, privateKey);

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

    public static UploadTransResponse upload(String path) {
        UploadTransRequest uploadTransRequest = null;

            uploadTransRequest = new UploadTransRequest.Builder()
                    .setFrom(fromLan)
                    .setTo(toLan)
                    //传入文件或者文件流都可
                    .setFile(new File(path))
                    //.setFileStream(new FileInputStream(path)).setFileName(docName)
                    .builder();
        return docTransOpenApiClient.excute(uploadTransRequest);
    }

    public static QueryTransProgressResponse queryTransProgress(Integer docId) {
        QueryTransProgressRequest queryTransProgressRequest = new QueryTransProgressRequest.Builder().setTid(docId).builder();
        return docTransOpenApiClient.excute(queryTransProgressRequest);
    }

    public static DownloadFileResponse downloadFile(Integer docId, Integer dType) {
        DownloadFileRequest downloadFileRequest = new DownloadFileRequest.Builder().setTid(docId).setDtype(dType).builder();
        return docTransOpenApiClient.excute(downloadFileRequest);
    }

    public  static DetectDocPageResponse detected(String path) throws FileNotFoundException {
        DetectDocPageRequest detectDocPageRequest  = new DetectDocPageRequest.Builder()
                .setFileStream(new FileInputStream(path)).setFileName(docName)
                ////传入文件或者文件流都可
                //.setFile(new File(path))
                .builder();

       return docTransOpenApiClient.excute(detectDocPageRequest);
    }

    public static SubmitForDetectDocResponse submitForDetectDoc(Integer tid){
        SubmitForDetectDocRequest submitForDetectDocRequest = new SubmitForDetectDocRequest.Builder().setTid(tid)
                .setFrom(fromLan)
                .setTo(toLan).builder();
        return docTransOpenApiClient.excute(submitForDetectDocRequest);

    }

    public static void transForDeteDocPage() throws FileNotFoundException, JsonProcessingException {
        DetectDocPageResponse docPageResponse  = detected(uploadFilePath);
        QueryTransProgressResponse queryTransProgressResponse;
        log.info("文档上传上传结果: {}",objectMapper.writeValueAsString(docPageResponse));
        if (docPageResponse.getTid()!=null){
            SubmitForDetectDocResponse submitForDetectDocResponse = submitForDetectDoc(docPageResponse.getTid());
            log.info("文档翻译上传结果: {}",objectMapper.writeValueAsString(submitForDetectDocResponse));
            boolean flag = true;
            while (flag) {
                try {
                    queryTransProgressResponse = queryTransProgress(docPageResponse.getTid());
                    log.info("文档翻译查询进度: {}", objectMapper.writeValueAsString(queryTransProgressResponse));
                    if (queryTransProgressResponse.getStatus().equals(314)) {
                        //下载格式转换后的文件
                        DownloadFileResponse response = downloadFile(queryTransProgressResponse.getTid(), dType);
                        //保存格式转换后得到的文件
                        bytesToFile(response.getBytes(), saveTransDirPath, saveTransFileName + response.getDocType());
                        return;
                    }
                    if (queryTransProgressResponse.getStatus().equals(313)) {
                        flag = true;
                    } else {
                        flag = false;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

    }


    public   static void trans()throws FileNotFoundException, JsonProcessingException {
        QueryTransProgressResponse queryTransProgressResponse;
        UploadTransResponse uploadTransResponse = upload(uploadFilePath);
        log.info("文档翻译上传结果: {}",objectMapper.writeValueAsString(uploadTransResponse));
        if (uploadTransResponse != null && uploadTransResponse.getTid() > 0) {
            boolean flag = true;
            while (flag) {
                try {
                    queryTransProgressResponse = queryTransProgress(uploadTransResponse.getTid());
                    log.info("文档翻译查询进度: {}", objectMapper.writeValueAsString(queryTransProgressResponse));
                    if (queryTransProgressResponse.getStatus().equals(314)) {
                        //下载格式转换后的文件
                        DownloadFileResponse response = downloadFile(queryTransProgressResponse.getTid(), dType);
                        //保存格式转换后得到的文件
                        bytesToFile(response.getBytes(), saveTransDirPath, saveTransFileName + response.getDocType());
                        return;
                    }
                    if (queryTransProgressResponse.getStatus().equals(313)) {
                        flag = true;
                    } else {
                        flag = false;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

        }
    }


    public static void main(String[] args) throws FileNotFoundException, JsonProcessingException {
        trans();
        //transForDeteDocPage();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy