Browse Source

加入测试用的接口

Yangzw 1 month ago
parent
commit
368a3fd9f7

+ 51 - 1
feifan-module-infra/feifan-module-infra-biz/src/main/java/cn/newfeifan/mall/module/infra/controller/admin/file/FileController.java

@@ -3,6 +3,7 @@ package cn.newfeifan.mall.module.infra.controller.admin.file;
 import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.core.util.URLUtil;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import cn.newfeifan.mall.framework.common.pojo.CommonResult;
 import cn.newfeifan.mall.framework.common.pojo.PageResult;
 import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
@@ -17,6 +18,8 @@ import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.Parameter;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import lombok.extern.slf4j.Slf4j;
+import okhttp3.OkHttpClient;
+import okhttp3.ResponseBody;
 import org.springframework.http.HttpStatus;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.validation.annotation.Validated;
@@ -29,6 +32,17 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
 
+import java.io.File;
+import java.io.IOException;
+import java.util.Map;
+
+import okhttp3.MediaType;
+import okhttp3.MultipartBody;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+
 import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;
 
 @Tag(name = "管理后台 - 文件存储")
@@ -73,7 +87,7 @@ public class FileController {
         }
         // 解码,解决中文路径的问题 https://gitee.com/zhijiantianya/ruoyi-vue-pro/pulls/807/
         path = URLUtil.decode(path);
-        log.info("=====path:{}",path);
+        log.info("=====path:{}", path);
 
         // 读取内容
         byte[] content = fileService.getFileContent(configId, path);
@@ -100,4 +114,40 @@ public class FileController {
         return success(BeanUtils.toBean(pageResult, FileRespVO.class));
     }
 
+    @GetMapping("/getCarNumber")
+    @Operation(summary = "获取识别的车牌号")
+    @PreAuthorize("@ss.hasPermission('infra:file:delete')")
+    public CommonResult<String> getCarNumber(@RequestParam("image_path") MultipartFile file) throws IOException {
+        OkHttpClient client = new OkHttpClient().newBuilder()
+                .build();
+        MediaType mediaType = MediaType.parse("application/octet-stream");
+        RequestBody fileRequestBody = RequestBody.create(mediaType, file.getBytes());
+
+        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
+                .addFormDataPart("image_path", file.getOriginalFilename(), fileRequestBody)
+                .build();
+        Request request = new Request.Builder()
+                .url("http://127.0.0.1:5000/recognize_plate")
+                .method("POST", body)
+                .addHeader("Authorization", "Bearer 964ae94dba204fa0bd3bcc5c27604d7e")
+                .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
+                .addHeader("Accept", "*/*")
+                .addHeader("Host", "127.0.0.1:5000")
+                .addHeader("Connection", "keep-alive")
+                .addHeader("Content-Type", "multipart/form-data; boundary=--------------------------174769765109986170531912")
+                .build();
+        Response response = client.newCall(request).execute();
+        if (response.isSuccessful()) {
+            ObjectMapper objectMapper = new ObjectMapper();
+            // 直接将响应体的字符串内容解析为Map(简单示例,如果有对应Java POJO类,可以解析为具体的POJO对象)
+            @SuppressWarnings("unchecked")
+            Map<String, Object> resultMap = objectMapper.readValue(response.body().string(), Map.class);
+            Object data = resultMap.get("data");
+            if (data.equals("success")) {
+                System.out.println("解析后的plate_number值: " + resultMap.get("plate_number"));
+                return success(resultMap.get("plate_number").toString());
+            }
+        }
+        return success("error");
+    }
 }