From 7a3b42bcb989e510a6e7a7b71b14162fe662cd0d Mon Sep 17 00:00:00 2001 From: qi Date: Tue, 11 Apr 2023 01:38:25 +0800 Subject: [PATCH] init project --- pom.xml | 91 +++++++++++++++++ src/main/java/com/ii2m/bill/App.java | 13 +++ src/main/java/com/ii2m/bill/Sample.java | 124 ++++++++++++++++++++++++ 3 files changed, 228 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/ii2m/bill/App.java create mode 100644 src/main/java/com/ii2m/bill/Sample.java diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a90f685 --- /dev/null +++ b/pom.xml @@ -0,0 +1,91 @@ + + + + 4.0.0 + + com.ii2m.bill + bill + 1.0-SNAPSHOT + + bill + + http://www.example.com + + + UTF-8 + 1.8 + 1.8 + + + + + junit + junit + 4.11 + test + + + + + com.squareup.okhttp3 + okhttp + 4.10.0 + + + + + com.alibaba + fastjson + 2.0.23 + + + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + diff --git a/src/main/java/com/ii2m/bill/App.java b/src/main/java/com/ii2m/bill/App.java new file mode 100644 index 0000000..f0301a7 --- /dev/null +++ b/src/main/java/com/ii2m/bill/App.java @@ -0,0 +1,13 @@ +package com.ii2m.bill; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/src/main/java/com/ii2m/bill/Sample.java b/src/main/java/com/ii2m/bill/Sample.java new file mode 100644 index 0000000..9663a73 --- /dev/null +++ b/src/main/java/com/ii2m/bill/Sample.java @@ -0,0 +1,124 @@ +package com.ii2m.bill; + +import com.alibaba.fastjson.JSONObject; +import okhttp3.*; + +import java.io.*; + +import java.math.BigDecimal; +import java.net.URLEncoder; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +class Sample { + public static final String API_KEY = ""; + public static final String SECRET_KEY = ""; + + static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build(); + + public static void main(String []args) throws IOException, InterruptedException { + //遍历文件 + List filePathList = new ArrayList<>(); + //TODO 修改文件目录 + String path = "I:\\bill"; + File file = new File(path); + fun(file,filePathList); + BigDecimal amount = new BigDecimal("0"); + for (String filePath:filePathList){ + // System.out.println(getFileContentAsBase64Urlencoded(filePath)); +// Thread.sleep(1000); + MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); + // pdf_file 可以通过 getFileContentAsBase64("C:\fakepath\A7F13B2F-A6E5-474D-85A5-ED52C4A76F5D.pdf") 方法获取,如果Content-Type是application/x-www-form-urlencoded时,使用getFileContentAsBase64Urlencoded方法获取 + String param = "pdf_file="; + if (filePath.indexOf(".pdf")==-1){ + param ="image="; + } + RequestBody body = RequestBody.create(mediaType,param+getFileContentAsBase64Urlencoded(filePath)); +// RequestBody body = RequestBody.create(mediaType,new File(path)); + Request request = new Request.Builder() + .url("https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice?access_token=" + getAccessToken()) + .method("POST", body) + .addHeader("Content-Type", "application/x-www-form-urlencoded") + .addHeader("Accept", "application/json") + .build(); + Response response = HTTP_CLIENT.newCall(request).execute(); + String bodyStr = response.body().string(); + JSONObject wordResult = JSONObject.parseObject(bodyStr).getJSONObject("words_result"); + if (wordResult!=null){ + BigDecimal totalAmount = wordResult.getBigDecimal("TotalAmount"); + if (totalAmount!=null) { + amount = amount.add(totalAmount); + }else { + System.out.println(bodyStr); + System.out.println(filePath+"\"结果异常!\""); + } + }else { + System.out.println(bodyStr); + System.out.println(filePath+"\"结果异常!\""); + } + + // System.out.println(response.body().string()); + } + System.out.println("总金额为:"); + System.out.println(amount); + + } + + /** + * 获取文件base64编码 + * @param path 文件路径 + * @return base64编码信息,不带文件头 + * @throws IOException IO异常 + */ + static String getFileContentAsBase64(String path) throws IOException { + byte[] b = Files.readAllBytes(Paths.get(path)); + return Base64.getEncoder().encodeToString(b); + } + /** + * 获取文件base64 UrlEncode编码 + * @param path 文件路径 + * @return base64编码信息,不带文件头 + * @throws IOException IO异常 + */ + static String getFileContentAsBase64Urlencoded(String path) throws IOException { + return URLEncoder.encode(getFileContentAsBase64(path), "utf-8"); + } + + + /** + * 从用户的AK,SK生成鉴权签名(Access Token) + * + * @return 鉴权签名(Access Token) + * @throws IOException IO异常 + */ + static String getAccessToken() throws IOException { + MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); + RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY + + "&client_secret=" + SECRET_KEY); + Request request = new Request.Builder() + .url("https://aip.baidubce.com/oauth/2.0/token") + .method("POST", body) + .addHeader("Content-Type", "application/x-www-form-urlencoded") + .build(); + Response response = HTTP_CLIENT.newCall(request).execute(); + return JSONObject.parseObject(response.body().string()).getString("access_token"); + // return new JSONObject(response.body().string()).getString("access_token"); + } + + private static void fun(File file,List filePathList){ + File[] fs = file.listFiles(); + for (File f:fs){ + if (f.isDirectory()){ + fun(f,filePathList); + } + if (f.isFile()){ + filePathList.add(f.getAbsolutePath()); + System.out.println(f.getAbsolutePath()); + } + } + } + +} \ No newline at end of file