|
@@ -0,0 +1,189 @@
|
|
|
|
|
+package com.dtm.common.utils.http;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.io.InputStream;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.core.io.FileSystemResource;
|
|
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
|
|
+import org.springframework.http.MediaType;
|
|
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
+import com.dtm.common.utils.StringUtils;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Flask API 客户端工具类
|
|
|
|
|
+ * 用于调用Flask后端API进行生命周期分析
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author dtm
|
|
|
|
|
+ */
|
|
|
|
|
+public class FlaskApiClient
|
|
|
|
|
+{
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(FlaskApiClient.class);
|
|
|
|
|
+
|
|
|
|
|
+ private static final String DEFAULT_FLASK_BASE_URL = "http://localhost:8085";
|
|
|
|
|
+ private static RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取Flask服务基础URL
|
|
|
|
|
+ */
|
|
|
|
|
+ private static String getFlaskBaseUrl()
|
|
|
|
|
+ {
|
|
|
|
|
+ // 优先从系统属性读取
|
|
|
|
|
+ String baseUrl = System.getProperty("flask.api.base.url");
|
|
|
|
|
+ if (StringUtils.isEmpty(baseUrl))
|
|
|
|
|
+ {
|
|
|
|
|
+ // 从环境变量读取
|
|
|
|
|
+ baseUrl = System.getenv("FLASK_API_BASE_URL");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.isEmpty(baseUrl))
|
|
|
|
|
+ {
|
|
|
|
|
+ // 使用默认值
|
|
|
|
|
+ baseUrl = DEFAULT_FLASK_BASE_URL;
|
|
|
|
|
+ }
|
|
|
|
|
+ return baseUrl;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 上传文件到Flask服务
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param file 上传的文件
|
|
|
|
|
+ * @return 响应结果
|
|
|
|
|
+ */
|
|
|
|
|
+ public static JSONObject uploadFile(MultipartFile file) throws Exception
|
|
|
|
|
+ {
|
|
|
|
|
+ String url = getFlaskBaseUrl() + "/api/upload";
|
|
|
|
|
+
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ // 将MultipartFile转换为临时文件
|
|
|
|
|
+ File tempFile = convertMultipartFileToFile(file);
|
|
|
|
|
+
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ // 创建请求体
|
|
|
|
|
+ MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
|
|
|
|
+ FileSystemResource resource = new FileSystemResource(tempFile);
|
|
|
|
|
+ body.add("file", resource);
|
|
|
|
|
+
|
|
|
|
|
+ // 设置请求头
|
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
+ headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
|
|
+
|
|
|
|
|
+ HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
|
|
|
|
|
+
|
|
|
|
|
+ // 发送请求
|
|
|
|
|
+ log.info("调用Flask API上传文件: {}", url);
|
|
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
|
|
|
|
|
+
|
|
|
|
|
+ // 解析响应
|
|
|
|
|
+ JSONObject result = JSON.parseObject(response.getBody());
|
|
|
|
|
+ log.info("Flask API上传文件响应: {}", result);
|
|
|
|
|
+
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+ finally
|
|
|
|
|
+ {
|
|
|
|
|
+ // 删除临时文件
|
|
|
|
|
+ if (tempFile.exists())
|
|
|
|
|
+ {
|
|
|
|
|
+ tempFile.delete();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e)
|
|
|
|
|
+ {
|
|
|
|
|
+ log.error("调用Flask API上传文件失败: {}", e.getMessage(), e);
|
|
|
|
|
+ throw new Exception("上传文件到Flask服务失败: " + e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 分析文件
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return 分析结果
|
|
|
|
|
+ */
|
|
|
|
|
+ public static JSONObject analyzeFile() throws Exception
|
|
|
|
|
+ {
|
|
|
|
|
+ String url = getFlaskBaseUrl() + "/api/analyze";
|
|
|
|
|
+
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
+ HttpEntity<String> requestEntity = new HttpEntity<>(headers);
|
|
|
|
|
+
|
|
|
|
|
+ log.info("调用Flask API分析文件: {}", url);
|
|
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject result = JSON.parseObject(response.getBody());
|
|
|
|
|
+ log.info("Flask API分析文件响应: {}", result);
|
|
|
|
|
+
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e)
|
|
|
|
|
+ {
|
|
|
|
|
+ log.error("调用Flask API分析文件失败: {}", e.getMessage(), e);
|
|
|
|
|
+ throw new Exception("分析文件失败: " + e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取分析结果
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return 分析结果
|
|
|
|
|
+ */
|
|
|
|
|
+ public static JSONObject getResults() throws Exception
|
|
|
|
|
+ {
|
|
|
|
|
+ String url = getFlaskBaseUrl() + "/api/results";
|
|
|
|
|
+
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
+ HttpEntity<String> requestEntity = new HttpEntity<>(headers);
|
|
|
|
|
+
|
|
|
|
|
+ log.info("调用Flask API获取结果: {}", url);
|
|
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject result = JSON.parseObject(response.getBody());
|
|
|
|
|
+ log.info("Flask API获取结果响应: {}", result);
|
|
|
|
|
+
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e)
|
|
|
|
|
+ {
|
|
|
|
|
+ log.error("调用Flask API获取结果失败: {}", e.getMessage(), e);
|
|
|
|
|
+ throw new Exception("获取分析结果失败: " + e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 将MultipartFile转换为File
|
|
|
|
|
+ */
|
|
|
|
|
+ private static File convertMultipartFileToFile(MultipartFile multipartFile) throws IOException
|
|
|
|
|
+ {
|
|
|
|
|
+ File file = File.createTempFile("upload_", "_" + multipartFile.getOriginalFilename());
|
|
|
|
|
+ try (FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
|
+ InputStream is = multipartFile.getInputStream())
|
|
|
|
|
+ {
|
|
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
|
|
+ int bytesRead;
|
|
|
|
|
+ while ((bytesRead = is.read(buffer)) != -1)
|
|
|
|
|
+ {
|
|
|
|
|
+ fos.write(buffer, 0, bytesRead);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return file;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|