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){ // 获得Http客户端 CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 创建Post请求 //设置请求路径 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); // 响应模型(发送post请求) 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"; // 将图片转换为Base64编码 byte[] imageBytes = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(imagePath)); String base64Image = Base64.getEncoder().encodeToString(imageBytes); // 创建URL对象 URL url = new URL("http://10.3.2.70:8115"); //打开HTTP连接 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); // 将Base64编码的图片作为请求体发送 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 json.put("errcode","0000"); json.put("traceId",""); json.put("description","操作成功"); JSONObject data = new JSONObject();//数据层json json.put("batchNo",""); JSONArray recoginitionData = new JSONArray();//数据层数组 if("".equals(Params)){return null;}; JSONArray objects = JSONArray.parseArray(Params); for(int a=0;a