From 9f64646c776a35c72e4565d8cfcea9c28417bd44 Mon Sep 17 00:00:00 2001 From: qiaoqinzheng Date: Tue, 5 Dec 2023 09:22:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=BA=E5=B7=A5=E4=BA=A4=E4=BA=92=E5=88=86?= =?UTF-8?q?=E6=9E=90gamma=E9=87=8D=E6=96=B0=E5=88=86=E6=9E=90=E5=B3=B0?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=B0=83=E7=94=A8=E7=AE=97=E6=B3=95=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BB=A3=E7=A0=81=E4=BF=AE=E6=94=B9=EF=BC=8Cdll?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E8=B0=83=E7=94=A8http=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/jeecg/common/handler/HttpClient.java | 103 ++++++++++++++++++ .../service/impl/GammaServiceImpl.java | 3 +- 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 jeecg-boot-base-core/src/main/java/org/jeecg/common/handler/HttpClient.java diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/common/handler/HttpClient.java b/jeecg-boot-base-core/src/main/java/org/jeecg/common/handler/HttpClient.java new file mode 100644 index 00000000..0a950086 --- /dev/null +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/common/handler/HttpClient.java @@ -0,0 +1,103 @@ +package org.jeecg.common.handler; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Objects; + +public class HttpClient { + private static final String URL_ADDRESS = "http://127.0.0.1:7080/"; + + public static String fitPeakFull(String phdStr, List Af, List Cf, List Ff) { + //接收算法返回内容 + String resultJson = ""; + //拼接请求路径 + String url = URL_ADDRESS + "fitPeakFull"; + HttpURLConnection connection = null; + OutputStream out = null; + BufferedReader in = null; + try { + //创建请求参数 以json的形式 + JsonObject requestJson = new JsonObject(); + requestJson.addProperty("phd", phdStr); + JsonArray AfJson = new JsonArray(); + for (int i = 0; i < Af.size(); i++) { + AfJson.add(Af.get(i)); + } + requestJson.add("Af", AfJson); + JsonArray CfJson = new JsonArray(); + for (int i = 0; i < Cf.size(); i++) { + CfJson.add(Cf.get(i)); + } + requestJson.add("Cf", CfJson); + JsonArray FfJson = new JsonArray(); + for (int i = 0; i < Ff.size(); i++) { + FfJson.add(Ff.get(i)); + } + requestJson.add("Ff", FfJson); + //请求的json参数转换为字符串 + String requestParam = requestJson.toString(); + //请求的json字符串转为字节数组的方式 + byte[] requestParamBytes = requestParam.getBytes(StandardCharsets.UTF_8); + //建立Http连接 + connection = (HttpURLConnection) new URL(url).openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestProperty("Content-Length", String.valueOf(requestParamBytes.length)); + connection.setDoOutput(true); + connection.setConnectTimeout(300000); + connection.setReadTimeout(300000); + //获取http连接的输出流 + out = connection.getOutputStream(); + //向输出流写入传递的参数字节数组信息 + out.write(requestParamBytes); + //接收http请求的响应编码 + int responseCode = connection.getResponseCode(); + //如果是成功的响应编码 + if (responseCode == HttpURLConnection.HTTP_OK) { + StringBuilder responseBuilder = new StringBuilder(); + in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); + String line; + while ((line = in.readLine()) != null) { + responseBuilder.append(line); + } + resultJson = responseBuilder.toString(); + } else { + System.out.println("Error while sending request to C++ server, response code: " + responseCode); + } + } catch (ProtocolException e) { + throw new RuntimeException(e); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + try { + if (Objects.nonNull(out)) { + out.close(); + } + if (Objects.nonNull(connection)) { + connection.disconnect(); + } + if (Objects.nonNull(in)) { + in.close(); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + return resultJson; + } + + +} diff --git a/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/service/impl/GammaServiceImpl.java b/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/service/impl/GammaServiceImpl.java index 8c0937db..7cd44626 100644 --- a/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/service/impl/GammaServiceImpl.java +++ b/jeecg-module-spectrum-analysis/src/main/java/org/jeecg/modules/service/impl/GammaServiceImpl.java @@ -23,6 +23,7 @@ import org.apache.shiro.SecurityUtils; import org.jeecg.common.api.vo.Result; import org.jeecg.common.cache.LocalCache; import org.jeecg.common.constant.DateConstant; +import org.jeecg.common.handler.HttpClient; import org.jeecg.common.properties.ParameterProperties; import org.jeecg.common.properties.SpectrumPathProperties; import org.jeecg.common.system.util.JwtUtil; @@ -1491,7 +1492,7 @@ public class GammaServiceImpl extends AbstractLogOrReport implements IGammaServi } ObjectMapper mapper = new ObjectMapper(); String phdStr = mapper.writeValueAsString(phd); - String strValue = CalValuesHandler.fitPeakFull(phdStr, Af, Cf, Ff); + String strValue = HttpClient.fitPeakFull(phdStr, Af, Cf, Ff); Map parseMap = JSON.parseObject(strValue, Map.class); for (Map.Entry entry : parseMap.entrySet()) { if (entry.getKey().equalsIgnoreCase("vPeak")) {