parent
fcfd4a8ea3
commit
7a3b42bcb9
3 changed files with 228 additions and 0 deletions
@ -0,0 +1,91 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
|
||||
<groupId>com.ii2m.bill</groupId> |
||||
<artifactId>bill</artifactId> |
||||
<version>1.0-SNAPSHOT</version> |
||||
|
||||
<name>bill</name> |
||||
<!-- FIXME change it to the project's website --> |
||||
<url>http://www.example.com</url> |
||||
|
||||
<properties> |
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
||||
<maven.compiler.source>1.8</maven.compiler.source> |
||||
<maven.compiler.target>1.8</maven.compiler.target> |
||||
</properties> |
||||
|
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>junit</groupId> |
||||
<artifactId>junit</artifactId> |
||||
<version>4.11</version> |
||||
<scope>test</scope> |
||||
</dependency> |
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --> |
||||
<dependency> |
||||
<groupId>com.squareup.okhttp3</groupId> |
||||
<artifactId>okhttp</artifactId> |
||||
<version>4.10.0</version> |
||||
</dependency> |
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> |
||||
<dependency> |
||||
<groupId>com.alibaba</groupId> |
||||
<artifactId>fastjson</artifactId> |
||||
<version>2.0.23</version> |
||||
</dependency> |
||||
|
||||
|
||||
</dependencies> |
||||
|
||||
<build> |
||||
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> |
||||
<plugins> |
||||
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> |
||||
<plugin> |
||||
<artifactId>maven-clean-plugin</artifactId> |
||||
<version>3.1.0</version> |
||||
</plugin> |
||||
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> |
||||
<plugin> |
||||
<artifactId>maven-resources-plugin</artifactId> |
||||
<version>3.0.2</version> |
||||
</plugin> |
||||
<plugin> |
||||
<artifactId>maven-compiler-plugin</artifactId> |
||||
<version>3.8.0</version> |
||||
</plugin> |
||||
<plugin> |
||||
<artifactId>maven-surefire-plugin</artifactId> |
||||
<version>2.22.1</version> |
||||
</plugin> |
||||
<plugin> |
||||
<artifactId>maven-jar-plugin</artifactId> |
||||
<version>3.0.2</version> |
||||
</plugin> |
||||
<plugin> |
||||
<artifactId>maven-install-plugin</artifactId> |
||||
<version>2.5.2</version> |
||||
</plugin> |
||||
<plugin> |
||||
<artifactId>maven-deploy-plugin</artifactId> |
||||
<version>2.8.2</version> |
||||
</plugin> |
||||
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> |
||||
<plugin> |
||||
<artifactId>maven-site-plugin</artifactId> |
||||
<version>3.7.1</version> |
||||
</plugin> |
||||
<plugin> |
||||
<artifactId>maven-project-info-reports-plugin</artifactId> |
||||
<version>3.0.0</version> |
||||
</plugin> |
||||
</plugins> |
||||
</pluginManagement> |
||||
</build> |
||||
</project> |
@ -0,0 +1,13 @@ |
||||
package com.ii2m.bill; |
||||
|
||||
/** |
||||
* Hello world! |
||||
* |
||||
*/ |
||||
public class App |
||||
{ |
||||
public static void main( String[] args ) |
||||
{ |
||||
System.out.println( "Hello World!" ); |
||||
} |
||||
} |
@ -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<String> 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<String> 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()); |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue