package com.ltkj.web.config.IdcardUtil;
|
|
|
import okhttp3.*;
|
import org.json.JSONObject;
|
|
import javax.imageio.stream.FileImageInputStream;
|
import java.io.*;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.util.List;
|
import java.util.Map;
|
/**
|
* @Company: 西安路泰科技有限公司
|
* @Author: zjh
|
* @Date: 2023/3/17 13:37
|
*/
|
public class IdcardUtil {
|
private static final char last2byte = (char) Integer.parseInt("00000011", 2);
|
private static final char last4byte = (char) Integer.parseInt("00001111", 2);
|
private static final char last6byte = (char) Integer.parseInt("00111111", 2);
|
private static final char lead6byte = (char) Integer.parseInt("11111100", 2);
|
private static final char lead4byte = (char) Integer.parseInt("11110000", 2);
|
private static final char lead2byte = (char) Integer.parseInt("11000000", 2);
|
private static final char[] encodeTable = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
|
|
public static String encode(byte[] from) {
|
StringBuilder to = new StringBuilder((int) ((double) from.length * 1.34D) + 3);
|
int num = 0;
|
char currentByte = 0;
|
|
int i;
|
for (i = 0; i < from.length; ++i) {
|
for (num %= 8; num < 8; num += 6) {
|
switch (num) {
|
case 0:
|
currentByte = (char) (from[i] & lead6byte);
|
currentByte = (char) (currentByte >>> 2);
|
case 1:
|
case 3:
|
case 5:
|
default:
|
break;
|
case 2:
|
currentByte = (char) (from[i] & last6byte);
|
break;
|
case 4:
|
currentByte = (char) (from[i] & last4byte);
|
currentByte = (char) (currentByte << 2);
|
if (i + 1 < from.length) {
|
currentByte = (char) (currentByte | (from[i + 1] & lead2byte) >>> 6);
|
}
|
break;
|
case 6:
|
currentByte = (char) (from[i] & last2byte);
|
currentByte = (char) (currentByte << 4);
|
if (i + 1 < from.length) {
|
currentByte = (char) (currentByte | (from[i + 1] & lead4byte) >>> 4);
|
}
|
}
|
|
to.append(encodeTable[currentByte]);
|
}
|
}
|
|
if (to.length() % 4 != 0) {
|
for (i = 4 - to.length() % 4; i > 0; --i) {
|
to.append("=");
|
}
|
}
|
|
return to.toString();
|
}
|
|
public static String post(String requestUrl, String accessToken, String params)
|
throws Exception {
|
String contentType = "application/x-www-form-urlencoded";
|
return IdcardUtil.post(requestUrl, accessToken, contentType, params);
|
}
|
|
public static String post(String requestUrl, String accessToken, String contentType, String params)
|
throws Exception {
|
String encoding = "UTF-8";
|
if (requestUrl.contains("nlp")) {
|
encoding = "GBK";
|
}
|
return IdcardUtil.post(requestUrl, accessToken, contentType, params, encoding);
|
}
|
|
public static String post(String requestUrl, String accessToken, String contentType, String params, String encoding)
|
throws Exception {
|
String url = requestUrl + "?access_token=" + accessToken;
|
return IdcardUtil.postGeneralUrl(url, contentType, params, encoding);
|
}
|
|
public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding)
|
throws Exception {
|
URL url = new URL(generalUrl);
|
// 打开和URL之间的连接
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
connection.setRequestMethod("POST");
|
// 设置通用的请求属性
|
connection.setRequestProperty("Content-Type", contentType);
|
connection.setRequestProperty("Connection", "Keep-Alive");
|
connection.setUseCaches(false);
|
connection.setDoOutput(true);
|
connection.setDoInput(true);
|
|
// 得到请求的输出流对象
|
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
|
out.write(params.getBytes(encoding));
|
out.flush();
|
out.close();
|
|
// 建立实际的连接
|
connection.connect();
|
// 获取所有响应头字段
|
Map<String, List<String>> headers = connection.getHeaderFields();
|
// 遍历所有的响应头字段
|
for (String key : headers.keySet()) {
|
System.err.println(key + "--->" + headers.get(key));
|
}
|
// 定义 BufferedReader输入流来读取URL的响应
|
BufferedReader in = null;
|
in = new BufferedReader(
|
new InputStreamReader(connection.getInputStream(), encoding));
|
String result = "";
|
String getLine;
|
while ((getLine = in.readLine()) != null) {
|
result += getLine;
|
}
|
in.close();
|
System.err.println("result:" + result);
|
return result;
|
}
|
|
/**
|
* 从用户的AK,SK生成鉴权签名(Access Token)
|
*
|
* @return 鉴权签名(Access Token)
|
* @throws IOException IO异常
|
*/
|
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
|
public 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=" +"mRFH6G6ILvXCpy1Pwgs1WZXW"
|
+ "&client_secret=" +"1o4DUvPuiGzPF44Y4gSsIrKMfn1awDhC");
|
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 new JSONObject(response.body().string()).getString("access_token");
|
}
|
/**
|
将图像转为二进制数组
|
* @return
|
*/
|
public static byte[] readImageFile(File file) {
|
byte[] data = null;
|
FileImageInputStream input = null;
|
try {
|
input = new FileImageInputStream(file);
|
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
byte[] buf = new byte[1024];
|
int numBytesRead = 0;
|
while ((numBytesRead = input.read(buf)) != -1) {
|
output.write(buf, 0, numBytesRead);
|
}
|
data = output.toByteArray();
|
output.close();
|
input.close();
|
} catch (FileNotFoundException ex1) {
|
ex1.printStackTrace();
|
} catch (IOException ex1) {
|
ex1.printStackTrace();
|
}
|
return data;
|
}
|
}
|