package com.sino.api.oldmethod.files.recog;
import jav***.File;
import jav***.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.htt***nsts;
import org.apache.http.HttpEntity;
import org.apache.http.c***thods.CloseableHttpResponse;
import org.apache.http.c***thods.HttpPost;
import org.apache.http.e***ntentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mim***ntent.FileBody;
import org.apache.http.entity.mim***ntent.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.CharsetUtils;
import org.apache.http.util.EntityUtils;
import com.sino.utils.JsonUtil;
import com.sino.utils.XmlParserX;
import com.sino.utils.XmlParserXfordoc;
/**发票识别
* 版本信息:V2.0.0.1
* 接口调用说明:
* 1. http接口地址="https://n***/api/v2/recogInvoive.do";
* http接口调用方法必须是post
* 2. http接口接收参数说明:
*/
public class Client {
public static void main(String[] args) {
File file = new File("D://a//01.jpg");
String key = "23jNhx********Kr2FLc"; // 用户ocrKey
String secret = "213fba54**********1d9133c22"; // 用户ocrSecret
String typeId = "20090"; //
String format = "json";
String url = "https://n***/api/v2/recogInvoive.do"; //http接口调用地址
String resultback = doPost(url, file, key, secret, typeId, format);
System.out.println(resultback); //控制台打印输出识别结果
}
public static String doPost(String url, File file, String key,String secret, String typeId, String format) {
String result = "";
try {
CloseableHttpClient client = HttpClients.createDefault(); // 1.创建httpclient对象
HttpPost post = new HttpPost(url); // 2.通过url创建post方法
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); //实例化实体构造器
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); //设置浏览器兼容模式
builder.addPart("file", new FileBody(file)); //添加"file"字段及其值;此处注意字段名称必须是"file"
builder.addPart("key", new StringBody(key, ContentType.create("text/plain", Consts.UTF_8))); //添加"key"字段及其值
builder.addPart("secret", new StringBody(secret, ContentType.create("text/plain", Consts.UTF_8))); //添加"secret"字段及其值
builder.addPart("typeId", new StringBody(typeId, ContentType.create("text/plain", Consts.UTF_8))); //添加"typeId"字段及其值
builder.addPart("format", new StringBody(format, ContentType.create("text/plain", Consts.UTF_8))); //添加"format"字段及其值
HttpEntity reqEntity = builder.setCharset(CharsetUtils.get("UTF-8")).build(); //设置请求的编码格式,并构造实体
post.setEntity(reqEntity);
CloseableHttpResponse response = client.execute(post); // 4.执行post方法,返回HttpResponse的对象
if (response.getStatusLine().getStatusCode() == 200) { // 5.如果返回结果状态码为200,则读取响应实体response对象的实体内容,并封装成String对象返回
result = EntityUtils.toString(response.getEntity(), "UTF-8");
} else {
System.out.println("服务器返回异常");
}
try {
HttpEntity e = response.getEntity(); // 6.关闭资源
if (e != null) {
InputStream instream = e.getContent();
instream.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return result; //7.返回识别结果
}
}
一种简单的发票识别接口调用方法
北京其他商务服务相关信息
2天前
2天前
2天前
2天前
2天前
2天前
3天前
3天前