|
@@ -0,0 +1,320 @@
|
|
|
+package nc.impl.th;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.DataOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.io.OutputStreamWriter;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+import nc.bs.framework.common.NCLocator;
|
|
|
+import nc.itf.uap.IVOPersistence;
|
|
|
+
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.apache.commons.httpclient.HttpClient;
|
|
|
+import org.apache.commons.httpclient.HttpException;
|
|
|
+import org.apache.commons.httpclient.methods.PostMethod;
|
|
|
+import org.apache.commons.httpclient.methods.RequestEntity;
|
|
|
+import org.apache.commons.httpclient.methods.StringRequestEntity;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+
|
|
|
+import nc.itf.th.IThOcrService;
|
|
|
+import nc.pub.toolkits.Toolkits;
|
|
|
+import nc.th.redistoken.RedisTokenManager;
|
|
|
+import nc.vo.pub.BusinessException;
|
|
|
+import nc.vo.th.ocr.OCRInvoiceVO;
|
|
|
+import net.sf.json.JSONString;
|
|
|
+/**
|
|
|
+ * OCR接口实现
|
|
|
+ * @author Administrator
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class ThOcrServiceImpl implements IThOcrService{
|
|
|
+
|
|
|
+ private IVOPersistence IVOPersistence = (IVOPersistence) NCLocator.getInstance().lookup(IVOPersistence.class);
|
|
|
+
|
|
|
+ public String[] insert(ArrayList<?> arrlist) throws BusinessException {
|
|
|
+ return this.IVOPersistence.insertVOList(arrlist);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ /**
|
|
|
+ * 获取token
|
|
|
+ */
|
|
|
+ public String getToken(String appId,Long cid,String secret,String userid) throws HttpException, IOException, BusinessException {
|
|
|
+
|
|
|
+ //redis中获取用户token
|
|
|
+ String userToken = RedisTokenManager.getToken(userid);
|
|
|
+ //token为空
|
|
|
+ if(Toolkits.isEmpty(userToken)){
|
|
|
+ //调用OCR接口
|
|
|
+ userToken=getOcrToken(appId,cid,secret,userid);
|
|
|
+ //保存到redis,设置失效时间36000秒
|
|
|
+ RedisTokenManager.setTokenTTL(userid,36000, userToken);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return userToken;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传发票
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public String uploadInvoice(String url,String token,File file) throws Exception {
|
|
|
+ String resmsg=httpFilePost(url,token,file);
|
|
|
+ String id="";
|
|
|
+ if(null!=resmsg&&resmsg.indexOf("uploading")!=-1){
|
|
|
+ JSONObject json = JSONObject.parseObject(resmsg);
|
|
|
+ JSONObject data=json.getJSONObject("data");
|
|
|
+ if(null!=data){
|
|
|
+ id=data.getString("id");
|
|
|
+ }else{
|
|
|
+ String msg=json.getJSONObject("res_base").getString("message");
|
|
|
+ throw new BusinessException(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return resmsg;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * OCR识别
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String ocrIdentification(String pathUrl, String token,String data) {
|
|
|
+ // TODO 自动生成的方法存根
|
|
|
+ JSONObject json = JSONObject.parseObject(data);
|
|
|
+ JSONObject jsonobject = new JSONObject();
|
|
|
+ jsonobject.put("image_index", json.getJSONObject("data").getString("id"));
|
|
|
+ jsonobject.put("fileTypeStr", json.getJSONObject("data").getString("fileType"));
|
|
|
+ jsonobject.put("flag", "13");
|
|
|
+ jsonobject.put("is_save", "0");
|
|
|
+ jsonobject.put("is_sync", "1");
|
|
|
+ jsonobject.put("ocrMap","");
|
|
|
+ jsonobject.put("operate_type",0);
|
|
|
+ return doPost(pathUrl,token,jsonobject.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * OCR识别数据保存数据库
|
|
|
+ * @throws BusinessException
|
|
|
+ */
|
|
|
+ public void insertOCRdataInfo(ArrayList<OCRInvoiceVO> vos) throws BusinessException {
|
|
|
+ insert(vos);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public String getOcrToken(String appId,Long cid,String secret,String userid) throws HttpException, IOException, BusinessException{
|
|
|
+ HttpClient httpClient = new HttpClient();
|
|
|
+ httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
|
|
|
+ httpClient.getHttpConnectionManager().getParams().setSoTimeout(5000);
|
|
|
+ PostMethod postMethod = new PostMethod("https://api.mypiaojia.com/api/invoiceApi/token/buildToken");
|
|
|
+ postMethod.setRequestHeader("content-type", "application/json");
|
|
|
+ postMethod.setRequestHeader("appkey", appId);
|
|
|
+ long l = System.currentTimeMillis();
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
+ map.put("role", 1);
|
|
|
+ map.put("name", "dmy06");
|
|
|
+ map.put("name", "dmy06");
|
|
|
+ map.put("userId", userid);//OA用户体系的用户id 2108
|
|
|
+ map.put("cid", cid);
|
|
|
+ String s1 = JSONObject.toJSONString(map);
|
|
|
+ String s2 = s1 + "&time=" + l + "&secret="+secret;
|
|
|
+ String newMd5 = DigestUtils.md5Hex(s2);
|
|
|
+ String s4 = s1 + "&time=" + l + "&md5=" + newMd5;
|
|
|
+ RequestEntity entity = new StringRequestEntity (s4 ,"application/json" ,"UTF-8");
|
|
|
+ postMethod.setRequestEntity(entity);
|
|
|
+ httpClient.executeMethod(postMethod);
|
|
|
+ String responseMsg = postMethod.getResponseBodyAsString().trim();
|
|
|
+ //返回结果{"data":"WEAPPe8Ke9pFzz9fbZ3qCcN6itq1zLHCKOgPmXHz6uIg8eOg","actionMsg":{"code":0,"message":"执行成功"}}
|
|
|
+ // System.out.println("返回结果"+ responseMsg);
|
|
|
+
|
|
|
+ JSONObject json = JSONObject.parseObject(responseMsg);
|
|
|
+ String token=json.getString("data");
|
|
|
+ String status=json.getString("status");
|
|
|
+ //保存token返回值到日志文件里
|
|
|
+
|
|
|
+
|
|
|
+ if(null!=status){
|
|
|
+ throw new BusinessException(responseMsg);
|
|
|
+ }
|
|
|
+
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public String httpFilePost(String uploadUrl,String token, File file) throws Exception {
|
|
|
+
|
|
|
+ //File file = new File(file);
|
|
|
+ String LINEND = "\r\n";
|
|
|
+ String boundary = UUID.randomUUID().toString();
|
|
|
+ FileInputStream fileInputStream = new FileInputStream(file);
|
|
|
+ URL url = new URL(uploadUrl);
|
|
|
+ HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
|
|
|
+ httpURLConnection.setDoInput(true);
|
|
|
+ httpURLConnection.setUseCaches(false);
|
|
|
+ httpURLConnection.setDoOutput(true);
|
|
|
+ httpURLConnection.setRequestMethod("POST");
|
|
|
+ httpURLConnection.setRequestProperty("connection", "keep-alive");
|
|
|
+ httpURLConnection.setRequestProperty("Charsert", "UTF-8");
|
|
|
+ httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
|
|
|
+ httpURLConnection.setRequestProperty("token", token);
|
|
|
+ httpURLConnection.setRequestProperty("Accept","*/*");
|
|
|
+ DataOutputStream outStream = new DataOutputStream(httpURLConnection.getOutputStream());
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
+ stringBuilder.append("--").append(boundary).append(LINEND);
|
|
|
+ stringBuilder.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + LINEND);
|
|
|
+ stringBuilder.append("Content-Type: " + HttpURLConnection.guessContentTypeFromName(file.getName()) + LINEND);
|
|
|
+ stringBuilder.append("Content-Transfer-Encoding: binary"+ LINEND);
|
|
|
+ stringBuilder.append(LINEND);
|
|
|
+ outStream.write(stringBuilder.toString().getBytes());
|
|
|
+ InputStream is = new FileInputStream(file);
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len = 0;
|
|
|
+ while ((len = is.read(buffer)) != -1){
|
|
|
+ outStream.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ is.close();
|
|
|
+ outStream.write(LINEND.getBytes());
|
|
|
+ byte[] end_data = ("--" + boundary + "--" + LINEND).getBytes();
|
|
|
+ outStream.write(end_data);
|
|
|
+ outStream.flush();
|
|
|
+ fileInputStream.close();
|
|
|
+ String response = readStream(httpURLConnection.getInputStream());
|
|
|
+ outStream.close();
|
|
|
+ httpURLConnection.disconnect();
|
|
|
+ return response;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private String readStream(InputStream inputStream) throws IOException {
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int bytesRead;
|
|
|
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+ byte[] bytes = outputStream.toByteArray();
|
|
|
+ String result = new String(bytes, "UTF-8");
|
|
|
+ outputStream.close();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public String doPost(String pathUrl, String token,String data){
|
|
|
+ OutputStreamWriter out = null;
|
|
|
+ BufferedReader br = null;
|
|
|
+ String result = "";
|
|
|
+ try {
|
|
|
+ URL url = new URL(pathUrl);
|
|
|
+ //打开和url之间的连接
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
+ //设定请求的方法为"POST",默认是GET
|
|
|
+ //post与get的不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
+
|
|
|
+ //设置30秒连接超时
|
|
|
+ conn.setConnectTimeout(30000);
|
|
|
+ //设置30秒读取超时
|
|
|
+ conn.setReadTimeout(30000);
|
|
|
+
|
|
|
+ // 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在http正文内,因此需要设为true, 默认情况下是false;
|
|
|
+ conn.setDoOutput(true);
|
|
|
+ // 设置是否从httpUrlConnection读入,默认情况下是true;
|
|
|
+ conn.setDoInput(true);
|
|
|
+
|
|
|
+ // Post请求不能使用缓存
|
|
|
+ conn.setUseCaches(false);
|
|
|
+
|
|
|
+ //设置通用的请求属性
|
|
|
+ conn.setRequestProperty("accept", "*/*");
|
|
|
+ conn.setRequestProperty("connection", "Keep-Alive"); //维持长链接
|
|
|
+ conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
|
|
|
+ conn.setRequestProperty("token", token);
|
|
|
+
|
|
|
+ //连接,从上述url.openConnection()至此的配置必须要在connect之前完成,
|
|
|
+ conn.connect();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下面的三句代码,就是调用第三方http接口
|
|
|
+ */
|
|
|
+ //获取URLConnection对象对应的输出流
|
|
|
+ //此处getOutputStream会隐含的进行connect(即:如同调用上面的connect()方法,所以在开发中不调用上述的connect()也可以)。
|
|
|
+ out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
|
|
|
+ //发送请求参数即数据
|
|
|
+ out.write(data);
|
|
|
+ //flush输出流的缓冲
|
|
|
+ out.flush();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下面的代码相当于,获取调用第三方http接口后返回的结果
|
|
|
+ */
|
|
|
+ //获取URLConnection对象对应的输入流
|
|
|
+ InputStream is = conn.getInputStream();
|
|
|
+ //构造一个字符流缓存
|
|
|
+ br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
|
|
|
+ String str = "";
|
|
|
+ while ((str = br.readLine()) != null){
|
|
|
+ result += str;
|
|
|
+ }
|
|
|
+ // System.out.println(result);
|
|
|
+
|
|
|
+ /* JSONObject json = JSONObject.parseObject(result);
|
|
|
+ //获取主要数据
|
|
|
+ JSONObject datajson = json.getJSONObject("data");
|
|
|
+ System.out.println("datajson===="+datajson.toString());
|
|
|
+ JSONArray infosjson = datajson.getJSONArray("infos");
|
|
|
+ System.out.println("infosjson===="+infosjson.toString());
|
|
|
+
|
|
|
+ for (Object infos : infosjson) {
|
|
|
+ JSONObject infosObject =(JSONObject)infos;
|
|
|
+ String total=infosObject.getJSONObject("info").getJSONObject("comm_info").getJSONObject("price").getString("total");
|
|
|
+ System.out.println("total===="+total);
|
|
|
+ }*/
|
|
|
+
|
|
|
+ //关闭流
|
|
|
+ is.close();
|
|
|
+ //断开连接,disconnect是在底层tcp socket链接空闲时才切断,如果正在被其他线程使用就不切断。
|
|
|
+ conn.disconnect();
|
|
|
+ }catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally {
|
|
|
+ try {
|
|
|
+ if (out != null){
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ if (br != null){
|
|
|
+ br.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|