123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- package kd.imc.rim;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import javassist.Loader;
- import kd.bos.dataentity.entity.DynamicObject;
- import kd.bos.orm.query.QCP;
- import kd.bos.orm.query.QFilter;
- import kd.bos.script.annotations.KSObject;
- import kd.bos.servicehelper.BusinessDataServiceHelper;
- import org.apache.http.HttpEntity;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.ContentType;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClientBuilder;
- import org.apache.http.util.EntityUtils;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.text.SimpleDateFormat;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.Base64;
- import java.util.Date;
- import java.util.Iterator;
- @KSObject
- public class ApiHttpUtils {
- public static String Posthttp(String url, String Params){
-
- CloseableHttpClient httpClient = HttpClientBuilder.create().build();
-
-
- HttpPost httpPost = new HttpPost(url);
- httpPost.setHeader("Content-type", "application/json;charset=utf-8");
- JSONObject object = new JSONObject();
- JSONObject head = new JSONObject();
- head.put("mesgtype","bills_crop_base64");
- head.put("channelcode","JSX");
- Date date = new Date();
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
- String formatDate = dateFormat.format(date);
- head.put("channeldate",formatDate);
- LocalDateTime now = LocalDateTime.now();
- DateTimeFormatter hHmmss = DateTimeFormatter.ofPattern("HHmmss");
- DateTimeFormatter yyyyMMddHHmmssSSSS = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSSS");
- String channeltime = now.format(hHmmss);
- String hHmmssSSS1 = now.format(yyyyMMddHHmmssSSSS);
- head.put("channeltime",channeltime);
- head.put("channelserno",hHmmssSSS1);
- head.put("bron","");
- head.put("tellerno","");
- head.put("reserve","");
- head.put("dealcode","");
- head.put("dealmsg","");
- head.put("App_key","XYK_DEJ_Key");
- head.put("APP_secret","XYK_DEJ_SECRET");
- JSONObject body = new JSONObject();
- body.put("file_base64",Params);
- object.put("head",head);
- object.put("body",body);
- StringEntity entity = new StringEntity(object.toString(), ContentType.APPLICATION_JSON);
- httpPost.setEntity(entity);
-
- CloseableHttpResponse response = null;
- try {
- response = httpClient.execute(httpPost);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
-
- HttpEntity responseEntity = response.getEntity();
- JSONObject jsonObject = new JSONObject();
- if (responseEntity != null) {
- try {
- jsonObject = JSON.parseObject(EntityUtils.toString(response.getEntity()));
- JSONArray jsonArray = jsonObject.getJSONObject("boby").getJSONArray("object_list");
- return jsonArray.toJSONString();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- if (httpClient != null) {
- try {
- httpClient.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- if (response != null) {
- try {
- response.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return "";
- }
- public static String HttpPostExample () {
- try {
- String imagePath = "path_to_your_image_file.jpg";
-
- byte[] imageBytes = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(imagePath));
- String base64Image = Base64.getEncoder().encodeToString(imageBytes);
-
- URL url = new URL("http://10.3.2.70:8115");
-
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("POST");
- connection.setRequestProperty("Content-Type", "application/json");
- connection.setRequestProperty("channelcode", "your_channel_code");
- connection.setRequestProperty("channeldate", "your_channel_date");
- connection.setDoOutput(true);
- try (OutputStream os = connection.getOutputStream())
- { os.write(base64Image.getBytes()); os.flush(); }
-
- int responseCode = connection.getResponseCode();
- if (responseCode == HttpURLConnection.HTTP_OK) {
- System.out.println("Request was successful.");
- } else { System.out.println("Request failed. Response Code: " + responseCode); }
-
- connection.disconnect(); }
- catch (Exception e) { e.printStackTrace(); }
- return null;
- }
- public static String toFiledata(String Params) throws Exception {
-
- JSONObject json = new JSONObject();
- json.put("errcode","0000");
- json.put("traceId","");
- json.put("description","操作成功");
- JSONObject data = new JSONObject();
- json.put("batchNo","");
- JSONArray recoginitionData = new JSONArray();
- if("".equals(Params)){return null;};
- JSONArray objects = JSONArray.parseArray(Params);
- for(int a=0;a<objects.size();a++){
- Object object = objects.get(a);
- JSONObject respenjson = (JSONObject) JSONObject.toJSON(object);
- String type_description = respenjson.getString("type_description");
- if("".equals(type_description)){return null;};
- JSONArray itemList = respenjson.getJSONArray("item_list");
- JSONObject item_list = new JSONObject();
- for (int i=0;i<itemList.size();i++){
- Object obj = itemList.get(i);
- JSONObject respenjsons = (JSONObject) JSONObject.toJSON(obj);
- String key = respenjsons.get("key").toString();
- String value = respenjsons.get("value").toString();
- item_list.put(key,value);
- }
- if("行程单".equals(type_description)){
- JSONArray flight_data_list = respenjson.getJSONArray("flight_data_list");
- for (int i=0;i<flight_data_list.size();i++){
- Object obj = flight_data_list.get(i);
- JSONArray xcobj = JSONArray.parseArray(obj.toString());
- for(int c=0;c<xcobj.size();c++){
- Object objc = flight_data_list.get(c);
- JSONObject respenjsons = (JSONObject) JSONObject.toJSON(objc);
- String key = respenjsons.get("key").toString();
- String value = respenjsons.get("value").toString();
- item_list.put(key,value);
- }
- }
- }
- QFilter nckd_File = new QFilter("nckd_filetype", QCP.equals, type_description);
- DynamicObject nckd_FileData = BusinessDataServiceHelper.loadSingle("nckd_filedataconvert", "id,nckd_filetype", new QFilter[]{nckd_File});
- JSONObject Filedata = FileData(nckd_FileData,item_list);
- recoginitionData.add(Filedata);
- }
- data.put("recoginitionData",recoginitionData);
- json.put("data",data);
- return json.toString();
- }
-
- public static JSONObject FileData(DynamicObject nckd_FileData,JSONObject item_list) throws Exception {
- JSONObject fileObj = new JSONObject();
- for (DynamicObject entryentity : nckd_FileData.getDynamicObjectCollection("nckd_treeentryentity")) {
- String nckd_keingde = entryentity.getString("nckd_keingde");
- String nckd_ocr = entryentity.getString("nckd_ocr");
- String nckd_acquiesce = entryentity.getString("nckd_acquiesce");
- if(!"".equals(nckd_ocr) && nckd_ocr!=null){
- fileObj.put(nckd_keingde,item_list.getString(nckd_ocr));
- }else if(!"".equals(nckd_acquiesce) && nckd_ocr!=null){
- fileObj.put(nckd_keingde,nckd_acquiesce);
- }else {
- fileObj.put(nckd_keingde,"");
- }
- }
- return fileObj;
- }
-
- public static String jpxcd(JSONObject item_list) throws Exception {
- return null;
- }
-
- public static String hcp(JSONObject item_list) throws Exception {
-
- JSONObject json = new JSONObject();
- json.put("errcode","0000");
- json.put("traceId","");
- json.put("description","操作成功");
- JSONObject data = new JSONObject();
- json.put("batchNo","");
- JSONArray recoginitionData = new JSONArray();
- JSONObject fileObj = new JSONObject();
- fileObj.put("canBeDeduction","");
- fileObj.put("stationGetOn",item_list.getString("departure_station"));
- fileObj.put("passengerName",item_list.getString("passenger_name"));
- fileObj.put("signStatus","");
- fileObj.put("downloadUrl","");
- fileObj.put("fileHash","");
- fileObj.put("seatNumber",item_list.getString("seat_number"));
- fileObj.put("localUrl","");
- fileObj.put("trainNum",item_list.getString("train_number"));
- fileObj.put("trainTime","");
- fileObj.put("stationGetOff",item_list.getString("arrival_station"));
- fileObj.put("deductionStatus",1);
- fileObj.put("invoiceType","9");
- fileObj.put("isRepeat",false);
- fileObj.put("pixel","");
- fileObj.put("oriImageSize","");
- fileObj.put("pdfToImgSnapshotUrl","");
- fileObj.put("orientation","0");
- fileObj.put("batchNo","");
- fileObj.put("warningCode","1");
- fileObj.put("originalState",0);
- fileObj.put("originalUrl","");
- fileObj.put("invoiceDate",item_list.getString("departure_date"));
- fileObj.put("serialNo","");
- fileObj.put("seat",item_list.getString("class"));
- fileObj.put("totalAmount",item_list.getString("price"));
- fileObj.put("taxRate","");
- fileObj.put("customerIdentityNum",item_list.getString("passenger_id"));
- fileObj.put("oriOrientation","0");
- fileObj.put("oriRegion","0");
- fileObj.put("rotationAngle","");
- fileObj.put("snapshotUrl","");
- fileObj.put("imageSerialNo","");
- fileObj.put("printingSequenceNo",item_list.getString("number"));
- fileObj.put("recognitionSerialNo","");
- fileObj.put("businessType",1);
- fileObj.put("region","");
-
- recoginitionData.add(fileObj);
- data.put("recoginitionData",recoginitionData);
- json.put("data",data);
- return json.toJSONString();
- }
-
- public static String tydefp(JSONObject item_list) throws Exception {
- return null;
- }
- }
|