文档

Java SDK使用指南

更新时间:

准备Java SDK环境

在使用Java SDK前,您需要配置好Java SDK环境,Java版本要求Java 8及以上。

然后在Maven项目的pom.xml文件中,添加阿里云sdk核心库tea-openapi、文档智能sdk docmind_api20220711依赖,如下所示。

<dependencies>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>tea-openapi</artifactId>
        <version>0.2.5</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>docmind_api20220711</artifactId>
        <version>1.2.1</version>
    </dependency>
</dependencies> 

配置身份认证

  1. 创建AccessKey。

    详细介绍,请参见创建AccessKey

    重要

    阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。使用RAM用户时,需授予RAM用户文档智能相关的访问权限,再使用RAM用户的AccessKey调用SDK。详情请参见通过RAM用户控制文档智能使用权限

    强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。

    如果担心RAM用户的AccessKey泄露,可以考虑通过创建RAM角色并使用STS临时授权的账号调用服务,详情请参见通过STS临时授权的账号调用服务

  2. 添加阿里云SDK Credentials依赖。

    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>credentials-java</artifactId>
        <version>0.2.11</version>
    </dependency>
  3. 配置身份认证。

    本文以配置文件的方式为例。详细操作,请参见配置方案

调用接口提交文档处理任务

文档智能提供异步任务接口,Java SDK提供了本地文档上传和传入文档URL这两种调用方式。

若您需要识别的文件为大文件,耗时较长。您可对config对象设置以下属性。

// 建立连接超时时间
config.connectTimeout = 60000;
// 读取资源超时时间
config.readTimeout = 60000;

关于文档智能提供的所有OpenAPI,请参见API概览

使用本地文档提交异步任务

图片转Word、图片转Excel、图片转PDF接口不支持本地文件上传方式。

以下代码示例表示通过本地文件上传方式调用文档智能解析接口的异步任务提交类API,其他接口类似。

import com.aliyun.docmind_api20220711.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.docmind_api20220711.Client;
import com.aliyun.teautil.models.RuntimeOptions;
import java.io.File;
import java.io.FileInputStream;

public static void submit() throws Exception {
    // 调用接口时,程序直接访问凭证,读取您的访问密钥(即AccessKey)并自动完成鉴权。
    // 运行本示例前,请先完成步骤二:配置身份认证。
    // 本示例使用默认配置文件方式,通过配置Credentials文件创建默认的访问凭证。
    // 使用默认凭证初始化Credentials Client。
    com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
    Config config = new Config()
        // 通过Credentials获取配置中的AccessKey ID。
        .setAccessKeyId(credentialClient.getAccessKeyId())
        // 通过Credentials获取配置中的AccessKey Secret。
        .setAccessKeySecret(credentialClient.getAccessKeySecret());
    // 访问的域名,支持IPv4和IPv6两种方式,IPv6请使用docmind-api-dualstack.cn-hangzhou.aliyuncs.com。
    config.endpoint = "docmind-api.cn-hangzhou.aliyuncs.com";
    Client client = new Client(config);
    // 创建RuntimeObject实例并设置运行参数。
    RuntimeOptions runtime = new RuntimeOptions();
    // 替换成具体异步任务提交类API接口的入参和方法,示例方法是文档智能解析。
    SubmitDocStructureJobAdvanceRequest advanceRequest = new SubmitDocStructureJobAdvanceRequest();
    File file = new File("D:\\example.pdf");
    advanceRequest.fileUrlObject = new FileInputStream(file);
    advanceRequest.fileName = "example.pdf";
    // 4 发起请求并处理应答或异常。
    SubmitDocStructureJobResponse response = client.submitDocStructureJobAdvance(advanceRequest, runtime);
}

返回结果如下所示,其中Id表示用于后续查询处理结果的流水号。

{
  "RequestId": "4FF7D611-782B-1557-AF71-6541E10A****",
  "Data": {
    "Id": "docmind-20220902-824b****"
  }
}

传入文档URL提交任务

您传入的文档URL必须为公网可访问下载的公网URL地址,无跨域限制,URL不带特殊转移字符。

以下代码示例表示通过文档URL方式调用文档智能解析接口的异步任务提交类API,其他接口类似。

import com.aliyun.docmind_api20220711.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.docmind_api20220711.Client;

public static void submit() throws Exception {
    // 调用接口时,程序直接访问凭证,读取您的访问密钥(即AccessKey)并自动完成鉴权。
    // 运行本示例前,请先完成步骤二:配置身份认证。
    // 本示例使用默认配置文件方式,通过配置Credentials文件创建默认的访问凭证。
    // 使用默认凭证初始化Credentials Client。
    com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
    Config config = new Config()
        // 通过Credentials获取配置中的AccessKey ID。
        .setAccessKeyId(credentialClient.getAccessKeyId())
        // 通过Credentials获取配置中的AccessKey Secret。
        .setAccessKeySecret(credentialClient.getAccessKeySecret());
    // 访问的域名,支持IPv4和IPv6两种方式,IPv6请使用docmind-api-dualstack.cn-hangzhou.aliyuncs.com。
    config.endpoint = "docmind-api.cn-hangzhou.aliyuncs.com";
    Client client = new Client(config);
    // 替换成具体异步任务提交类API接口的入参和方法,示例方法是文档智能解析。
    SubmitDocStructureJobRequest request = new SubmitDocStructureJobRequest();
    request.fileName = "example.pdf";
    request.fileUrl = "https://example.com/example.pdf";
    SubmitDocStructureJobResponse response = client.submitDocStructureJob(request);
}

返回结果如下所示,其中Id表示用于后续查询处理结果的流水号。

{
  "RequestId": "4FF7D611-782B-1557-AF71-6541E10A****",
  "Data": {
    "Id": "docmind-20220902-824b****"
  }
}

调用结果查询类API

针对结果查询类API,查询结果有处理中、处理成功、处理失败三种情况。

以下代码示例表示调用文档智能解析接口的结果查询类API,其他接口类似。

import com.aliyun.docmind_api20220711.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.docmind_api20220711.Client;

public static void submit() throws Exception {
    // 调用接口时,程序直接访问凭证,读取您的访问密钥(即AccessKey)并自动完成鉴权。
    // 运行本示例前,请先完成步骤二:配置身份认证。
    // 本示例使用默认配置文件方式,通过配置Credentials文件创建默认的访问凭证。
    // 使用默认凭证初始化Credentials Client。
    com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
    Config config = new Config()
        // 通过Credentials获取配置中的AccessKey ID。
        .setAccessKeyId(credentialClient.getAccessKeyId())
        // 通过Credentials获取配置中的AccessKey Secret。
        .setAccessKeySecret(credentialClient.getAccessKeySecret());
    // 访问的域名,支持IPv4和IPv6两种方式,IPv6请使用docmind-api-dualstack.cn-hangzhou.aliyuncs.com。
    config.endpoint = "docmind-api.cn-hangzhou.aliyuncs.com";
    Client client = new Client(config);
    GetDocStructureResultRequest resultRequest  = new GetDocStructureResultRequest();
    resultRequest.id = "docmind-20220902-824b****";
    // 替换成具体结果查询类API接口,示例方法是文档智能解析。
    GetDocStructureResultResponse response = client.getDocStructureResult(resultRequest);
    System.out.println(response.getBody().getData());
}

返回结果分为处理中、处理成功、处理失败三种情况。

处理中的返回结果如下所示,Completed会返回false,表示任务没有处理结束,仍在处理中。这种需要继续轮询,直到明确返回Completed为true或者超过轮询最大时间。

{
  "RequestId": "2AABD2C2-D24F-12F7-875D-683A27C3****",
  "Completed": false,
  "Code": "DocProcessing",
  "Message": "Document processing",
  "HostId": "ocr-api.cn-hangzhou.aliyuncs.com",
  "Recommend": "https://next.api.aliyun.com/troubleshoot?q=DocProcessing&product=docmind-api"
}

处理成功的返回结果如下所示,Completed会返回true,表示任务处理结束,同时会返回Status为字符串的Success,表示处理成功。具体的处理结果在Data节点中。

{
  "Status": "Success",
  "RequestId": "73134E1A-E281-1B2C-A105-D0ECFE2DFail",
  "Completed": true,
	"Data": {
		"styles": [{
				"styleId": 0,
				"underline": false,
				"deleteLine": false,
				"bold": true,
				"italic": false,
				"fontSize": 15,
				"fontName": "黑体",
				"color": "000000",
				"charScale": 0.95
			},
			{
				"styleId": 1,
				"underline": false,
				"deleteLine": false,
				"bold": false,
				"italic": false,
				"fontSize": 12,
				"fontName": "微软雅黑",
				"color": "000000",
				"charScale": 1
			}
		],
		"layouts": [{
			"text": "测试标题",
			"index": 0,
			"uniqueId": "xxxx9816e77caea338df554b80ab95c7",
			"alignment": "center",
			"pageNum": [
				0
			],
			"pos": [{
					"x": 405,
					"y": 192
				},
				{
					"x": 860,
					"y": 191
				},
				{
					"x": 860,
					"y": 236
				},
				{
					"x": 406,
					"y": 237
				}
			],
			"type": "title",
      "subType":"doc_title"
		}, {
			"text": "本段为测试内容",
			"index": 1,
			"uniqueId": "xxxx8606c213c01c12d70f98dcfb2525",
			"alignment": "left",
			"pageNum": [
				0
			],
			"pos": [{
					"x": 187,
					"y": 311
				},
				{
					"x": 1075,
					"y": 311
				},
				{
					"x": 1076,
					"y": 373
				},
				{
					"x": 187,
					"y": 373
				}
			],
			"type": "text",
      "subType":"para",
			"lineHeight": 7,
			"firstLinesChars": 30,
			"blocks": [{
					"text": "本段",
					"pos": null,
					"styleId": 0
				},
				{
					"text": "为测试内容",
					"pos": null,
					"styleId": 1
				}
			]
		}],
		"logics": {
			"docTree": [{
				"uniqueId": "xxxx9816e77caea338df554b80ab95c7",
				"level": 0,
				"link": {
					"下级": [

					],
					"包含": [

					]
				},
				"backlink": {
					"上级": [
						"ROOT"
					]
				}
			}],
			"paragraphKVs": null,
			"tableKVs": null
		},
		"docInfo": {
			"docType": "pdf",
			"orignalDocName": "1.pdf",
			"pages": [{
				"imageType": "JPEG",
				"imageUrl": "http://test.moshi.aliyuncs.com/docMind/image/xxxx3cccbfec45b48d3a8081c9c9659e/0",
				"angle": null,
				"imageWidth": 1273,
				"imageHeight": 1801,
				"pageIdCurDoc": 1,
				"pageIdAllDocs": 1
			}]
		}
	}
}

处理失败Completed会返回true,表示任务处理结束,同时会返回Status为字符串的Fail,表示处理成功失败,同时会返回失败Code和详细原因Message。访问错误码可以查看错误码详细介绍。

{
  "RequestId": "A8EF3A36-1380-1116-A39E-B377BE27****",
  "Completed": true,
  "Status": "Fail",
  "Code": "UrlNotLegal",
  "Message": "Failed to process the document.  The document url you provided is not legal.",
  "HostId": "docmind-api.cn-hangzhou.aliyuncs.com",
  "Recommend": "https://next.api.aliyun.com/troubleshoot?q=IDP.UrlNotLegal&product=docmind-api"
}
  • 本页导读 (0)
文档反馈