ApiHttpUtils.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package kd.imc.rim;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import javassist.Loader;
  6. import kd.bos.dataentity.entity.DynamicObject;
  7. import kd.bos.orm.query.QCP;
  8. import kd.bos.orm.query.QFilter;
  9. import kd.bos.script.annotations.KSObject;
  10. import kd.bos.servicehelper.BusinessDataServiceHelper;
  11. import org.apache.http.HttpEntity;
  12. import org.apache.http.client.methods.CloseableHttpResponse;
  13. import org.apache.http.client.methods.HttpPost;
  14. import org.apache.http.entity.ContentType;
  15. import org.apache.http.entity.StringEntity;
  16. import org.apache.http.impl.client.CloseableHttpClient;
  17. import org.apache.http.impl.client.HttpClientBuilder;
  18. import org.apache.http.util.EntityUtils;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.net.HttpURLConnection;
  22. import java.net.URL;
  23. import java.text.SimpleDateFormat;
  24. import java.time.LocalDateTime;
  25. import java.time.format.DateTimeFormatter;
  26. import java.util.Base64;
  27. import java.util.Date;
  28. import java.util.Iterator;
  29. @KSObject
  30. public class ApiHttpUtils {
  31. public static String Posthttp(String url, String Params){
  32. // 获得Http客户端
  33. CloseableHttpClient httpClient = HttpClientBuilder.create().build();
  34. // 创建Post请求
  35. //设置请求路径
  36. HttpPost httpPost = new HttpPost(url);
  37. httpPost.setHeader("Content-type", "application/json;charset=utf-8");
  38. JSONObject object = new JSONObject();
  39. JSONObject head = new JSONObject();
  40. head.put("mesgtype","bills_crop_base64");
  41. head.put("channelcode","JSX");
  42. Date date = new Date();
  43. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
  44. String formatDate = dateFormat.format(date);
  45. head.put("channeldate",formatDate);
  46. LocalDateTime now = LocalDateTime.now();
  47. DateTimeFormatter hHmmss = DateTimeFormatter.ofPattern("HHmmss");
  48. DateTimeFormatter yyyyMMddHHmmssSSSS = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSSS");
  49. String channeltime = now.format(hHmmss);
  50. String hHmmssSSS1 = now.format(yyyyMMddHHmmssSSSS);
  51. head.put("channeltime",channeltime);
  52. head.put("channelserno",hHmmssSSS1);
  53. head.put("bron","");
  54. head.put("tellerno","");
  55. head.put("reserve","");
  56. head.put("dealcode","");
  57. head.put("dealmsg","");
  58. head.put("App_key","XYK_DEJ_Key");
  59. head.put("APP_secret","XYK_DEJ_SECRET");
  60. JSONObject body = new JSONObject();
  61. body.put("file_base64",Params);
  62. object.put("head",head);
  63. object.put("body",body);
  64. StringEntity entity = new StringEntity(object.toString(), ContentType.APPLICATION_JSON);
  65. httpPost.setEntity(entity);
  66. // 响应模型(发送post请求)
  67. CloseableHttpResponse response = null;
  68. try {
  69. response = httpClient.execute(httpPost);
  70. } catch (IOException e) {
  71. throw new RuntimeException(e);
  72. }
  73. // 从响应模型中获取响应实体
  74. HttpEntity responseEntity = response.getEntity();
  75. JSONObject jsonObject = new JSONObject();
  76. if (responseEntity != null) {
  77. try {
  78. jsonObject = JSON.parseObject(EntityUtils.toString(response.getEntity()));
  79. JSONArray jsonArray = jsonObject.getJSONObject("boby").getJSONArray("object_list");
  80. return jsonArray.toJSONString();
  81. } catch (IOException e) {
  82. throw new RuntimeException(e);
  83. }
  84. }
  85. // 释放资源
  86. if (httpClient != null) {
  87. try {
  88. httpClient.close();
  89. } catch (IOException e) {
  90. throw new RuntimeException(e);
  91. }
  92. }
  93. if (response != null) {
  94. try {
  95. response.close();
  96. } catch (IOException e) {
  97. throw new RuntimeException(e);
  98. }
  99. }
  100. return "";
  101. }
  102. public static String HttpPostExample () {
  103. try { // 图片路径
  104. String imagePath = "path_to_your_image_file.jpg";
  105. // 将图片转换为Base64编码
  106. byte[] imageBytes = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(imagePath));
  107. String base64Image = Base64.getEncoder().encodeToString(imageBytes);
  108. // 创建URL对象
  109. URL url = new URL("http://10.3.2.70:8115");
  110. //打开HTTP连接
  111. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  112. connection.setRequestMethod("POST");
  113. connection.setRequestProperty("Content-Type", "application/json");
  114. connection.setRequestProperty("channelcode", "your_channel_code");
  115. connection.setRequestProperty("channeldate", "your_channel_date");
  116. connection.setDoOutput(true); // 将Base64编码的图片作为请求体发送
  117. try (OutputStream os = connection.getOutputStream())
  118. { os.write(base64Image.getBytes()); os.flush(); }
  119. //检查响应代码
  120. int responseCode = connection.getResponseCode();
  121. if (responseCode == HttpURLConnection.HTTP_OK) {
  122. System.out.println("Request was successful.");
  123. } else { System.out.println("Request failed. Response Code: " + responseCode); }
  124. // 关闭连接
  125. connection.disconnect(); }
  126. catch (Exception e) { e.printStackTrace(); }
  127. return null;
  128. }
  129. public static String toFiledata(String Params) throws Exception {
  130. //组装发票数据结构 !
  131. JSONObject json = new JSONObject();//返回最外层json
  132. json.put("errcode","0000");
  133. json.put("traceId","");
  134. json.put("description","操作成功");
  135. JSONObject data = new JSONObject();//数据层json
  136. json.put("batchNo","");
  137. JSONArray recoginitionData = new JSONArray();//数据层数组
  138. if("".equals(Params)){return null;};
  139. JSONArray objects = JSONArray.parseArray(Params);
  140. for(int a=0;a<objects.size();a++){
  141. Object object = objects.get(a);
  142. JSONObject respenjson = (JSONObject) JSONObject.toJSON(object);
  143. String type_description = respenjson.getString("type_description");//增值税电子普通发票 行程单 通用定额发票 火车票
  144. if("".equals(type_description)){return null;};
  145. JSONArray itemList = respenjson.getJSONArray("item_list");
  146. JSONObject item_list = new JSONObject();
  147. for (int i=0;i<itemList.size();i++){
  148. Object obj = itemList.get(i);
  149. JSONObject respenjsons = (JSONObject) JSONObject.toJSON(obj);
  150. String key = respenjsons.get("key").toString();// 获得key
  151. String value = respenjsons.get("value").toString();// 获得value
  152. item_list.put(key,value);
  153. }
  154. if("行程单".equals(type_description)){
  155. JSONArray flight_data_list = respenjson.getJSONArray("flight_data_list");
  156. for (int i=0;i<flight_data_list.size();i++){
  157. Object obj = flight_data_list.get(i);
  158. JSONArray xcobj = JSONArray.parseArray(obj.toString());
  159. for(int c=0;c<xcobj.size();c++){
  160. Object objc = flight_data_list.get(c);
  161. JSONObject respenjsons = (JSONObject) JSONObject.toJSON(objc);
  162. String key = respenjsons.get("key").toString();// 获得key
  163. String value = respenjsons.get("value").toString();// 获得value
  164. item_list.put(key,value);
  165. }
  166. }
  167. }
  168. QFilter nckd_File = new QFilter("nckd_filetype", QCP.equals, type_description);
  169. DynamicObject nckd_FileData = BusinessDataServiceHelper.loadSingle("nckd_filedataconvert", "id,nckd_filetype", new QFilter[]{nckd_File});
  170. JSONObject Filedata = FileData(nckd_FileData,item_list);
  171. recoginitionData.add(Filedata);
  172. }
  173. data.put("recoginitionData",recoginitionData);
  174. json.put("data",data);
  175. return json.toString();
  176. }
  177. //增值税电子普通发票
  178. public static JSONObject FileData(DynamicObject nckd_FileData,JSONObject item_list) throws Exception {
  179. JSONObject fileObj = new JSONObject();//实际数据json
  180. for (DynamicObject entryentity : nckd_FileData.getDynamicObjectCollection("nckd_treeentryentity")) {
  181. String nckd_keingde = entryentity.getString("nckd_keingde");
  182. String nckd_ocr = entryentity.getString("nckd_ocr");
  183. String nckd_acquiesce = entryentity.getString("nckd_acquiesce");
  184. if(!"".equals(nckd_ocr) && nckd_ocr!=null){
  185. fileObj.put(nckd_keingde,item_list.getString(nckd_ocr));
  186. }else if(!"".equals(nckd_acquiesce) && nckd_ocr!=null){
  187. fileObj.put(nckd_keingde,nckd_acquiesce);
  188. }else {
  189. fileObj.put(nckd_keingde,"");
  190. }
  191. }
  192. return fileObj;
  193. }
  194. //机票行程单
  195. public static String jpxcd(JSONObject item_list) throws Exception {
  196. return null;
  197. }
  198. //火车票
  199. public static String hcp(JSONObject item_list) throws Exception {
  200. //组装发票数据结构 !
  201. JSONObject json = new JSONObject();//返回最外层json
  202. json.put("errcode","0000");
  203. json.put("traceId","");
  204. json.put("description","操作成功");
  205. JSONObject data = new JSONObject();//数据层json
  206. json.put("batchNo","");
  207. JSONArray recoginitionData = new JSONArray();//数据层数组
  208. JSONObject fileObj = new JSONObject();//实际数据json
  209. fileObj.put("canBeDeduction","");//0
  210. fileObj.put("stationGetOn",item_list.getString("departure_station"));//出发站 ocr字段:departure_station
  211. fileObj.put("passengerName",item_list.getString("passenger_name"));//乘客 ocr字段:passenger_name
  212. fileObj.put("signStatus","");//0
  213. fileObj.put("downloadUrl","");//下载地址链接
  214. fileObj.put("fileHash","");//文件哈希值
  215. fileObj.put("seatNumber",item_list.getString("seat_number"));//座位 ocr字段:seat_number
  216. fileObj.put("localUrl","");//预览链接
  217. fileObj.put("trainNum",item_list.getString("train_number"));//车次编号 ocr字段:train_number
  218. fileObj.put("trainTime","");//
  219. fileObj.put("stationGetOff",item_list.getString("arrival_station"));//目的地 ocr字段:arrival_station
  220. fileObj.put("deductionStatus",1);
  221. fileObj.put("invoiceType","9");//发票类型
  222. fileObj.put("isRepeat",false);//是否重复
  223. fileObj.put("pixel","");//未知
  224. fileObj.put("oriImageSize","");//未知
  225. fileObj.put("pdfToImgSnapshotUrl","");//“”
  226. fileObj.put("orientation","0");//null
  227. fileObj.put("batchNo","");//
  228. fileObj.put("warningCode","1");//
  229. fileObj.put("originalState",0);//
  230. fileObj.put("originalUrl","");//""
  231. fileObj.put("invoiceDate",item_list.getString("departure_date"));//乘车日期 ocr字段:departure_date
  232. fileObj.put("serialNo","");//
  233. fileObj.put("seat",item_list.getString("class"));//几等座 ocr字段:class
  234. fileObj.put("totalAmount",item_list.getString("price"));//票价 ocr字段:price
  235. fileObj.put("taxRate","");//
  236. fileObj.put("customerIdentityNum",item_list.getString("passenger_id"));//身份证 ocr字段 passenger_id
  237. fileObj.put("oriOrientation","0");//
  238. fileObj.put("oriRegion","0");//
  239. fileObj.put("rotationAngle","");//未知字段
  240. fileObj.put("snapshotUrl","");//
  241. fileObj.put("imageSerialNo","");//未知字段
  242. fileObj.put("printingSequenceNo",item_list.getString("number"));//发票号码 ocr字段 number
  243. fileObj.put("recognitionSerialNo","");//
  244. fileObj.put("businessType",1);//未知字段
  245. fileObj.put("region","");//
  246. //值拼接完成后开始set数据结构e
  247. recoginitionData.add(fileObj);
  248. data.put("recoginitionData",recoginitionData);
  249. json.put("data",data);
  250. return json.toJSONString();
  251. }
  252. //通用定额发票
  253. public static String tydefp(JSONObject item_list) throws Exception {
  254. return null;
  255. }
  256. }