单独建一个包放下面的代码;
package com.p.service;
// 版权所有 © 艾科瑞特科技
// 艾科瑞特(iCREDIT)-让企业业绩长青
// 预知更多业绩长青,请与我们联系
// 联系电话:0532-88984128
// 联系邮箱:market@itruth.xin
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.apache.commons.codec.binary.Base64.encodeBase64;
//依赖请参照:https://icredit-code.oss-cn-hangzhou.aliyuncs.com/pom.xml
public class utils {
public static String main(String imgpath) throws IOException {
//API产品路径
String requestUrl = "https://iplatecard.market.alicloudapi.com/ai_market/ai_ocr_universal/license_plate/v2";
//阿里云APPCODE
String appcode = "66bec7664be849b08ffaaa4cd98df46f";
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "APPCODE " + appcode);
//根据API的要求,定义相对应的Content-Type
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
Map<String, String> bodys = new HashMap<String, String>();
//内容数据类型,如:0,则表示BASE64编码;1,则表示图像文件URL链接
//启用BASE64编码方式进行识别
//内容数据类型是BASE64编码
String imgBase64 = "";
try {
File file = new File(imgpath);
byte[] content = new byte[(int) file.length()];
FileInputStream finputstream = new FileInputStream(file);
finputstream.read(content);
finputstream.close();
imgBase64 = new String(encodeBase64(content));
} catch (IOException e) {
e.printStackTrace();
}
bodys.put("IMAGE", imgBase64);
bodys.put("IMAGE_TYPE", "0");
String response=post(requestUrl, headers, bodys);
System.out.println("response = " + response);
return response;
}
public static String post(String url, Map<String, String> headers, Map<String, String> body) throws IOException {
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(url);
// 必须设置下面这个Header
for (String key : headers.keySet()) {
postMethod.addRequestHeader(key, headers.get(key));
}
List<NameValuePair> bodyPair = new ArrayList<>();
for (String key : body.keySet()) {
bodyPair.add(new NameValuePair(key, body.get(key)));
}
NameValuePair[] bodyKvs = new NameValuePair[body.size()];
postMethod.setRequestBody(bodyPair.toArray(bodyKvs));
int code = client.executeMethod(postMethod);
if (code == 200) {
String res = postMethod.getResponseBodyAsString();
System.out.println(res);
return res;
}
else {
System.out.println(code);
}
throw new IOException("请求失败");
}
}
这些代码放到controller 里
public String insertPark(ParkingTable parking, Model model, @RequestParam(value = "file", required = false) MultipartFile file1) throws IOException {
String filePath = "D:\\Tupian"; //保存图片路径
String originalpath = file1.getOriginalFilename();//原路径
String newPath = UUID.randomUUID() + originalpath;
File targetFile = new File(filePath, newPath);//保存全路径
file1.transferTo(targetFile);//转移
// 调用上面的utils类;
String main = utils.main(targetFile.getPath());
String NUMBER="";
JSONObject json = JSONObject.fromObject(main);
JSONArray content = json.getJSONArray("CAR_NUMBER_RECOGNITION_ENTITY");
if (content.size() > 0) {
for (int i = 0; i < content.size(); i++) {
JSONObject job = content.getJSONObject(i);
NUMBER= (String) job.get("NUMBER");
}
}
.............
.......
}
<form enctype="multipart/form-data">
<input type="file" name="file">
</form>