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")) {