|
@@ -1,12 +1,14 @@
|
|
|
package com.doc.biz.controller;
|
|
|
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
import com.doc.biz.service.IMongoService;
|
|
|
import com.doc.biz.vo.DocumentVO;
|
|
|
import com.doc.common.core.controller.BaseController;
|
|
|
import com.doc.common.core.domain.AjaxResult;
|
|
|
+import com.doc.system.service.ISysConfigService;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
@@ -15,6 +17,10 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
import org.yaml.snakeyaml.util.UriEncoder;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
|
|
@@ -25,6 +31,7 @@ import java.util.Objects;
|
|
|
* @date 2023-08-15
|
|
|
*/
|
|
|
@Slf4j
|
|
|
+@Api(tags = "通用接口")
|
|
|
@RestController
|
|
|
@RequestMapping("/api")
|
|
|
public class ApiController extends BaseController {
|
|
@@ -34,6 +41,8 @@ public class ApiController extends BaseController {
|
|
|
*/
|
|
|
@Resource
|
|
|
private IMongoService mongoService;
|
|
|
+ @Resource
|
|
|
+ private ISysConfigService configService;
|
|
|
|
|
|
/**
|
|
|
* 文件下载
|
|
@@ -57,47 +66,63 @@ public class ApiController extends BaseController {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 文件上传
|
|
|
+ * 文字识别
|
|
|
*
|
|
|
- * @param file
|
|
|
+ * @param fileId fileId
|
|
|
* @return
|
|
|
*/
|
|
|
- @PostMapping("/upload")
|
|
|
- public AjaxResult uploadFile(@RequestParam(value = "file") MultipartFile file) {
|
|
|
+ @ApiOperation("文字识别-通过fileId")
|
|
|
+ @GetMapping("/ocr/{fileId}")
|
|
|
+ public AjaxResult ocr(@PathVariable(name = "fileId") String fileId) {
|
|
|
+ DocumentVO vo = mongoService.downloadFile(fileId);
|
|
|
try {
|
|
|
- return success(mongoService.uploadFile(file));
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("文件上传失败:", e);
|
|
|
+ File upFile = File.createTempFile(vo.getFileName(), vo.getSuffix() + ".");
|
|
|
+ Files.write(upFile.toPath(), vo.getData());
|
|
|
+ String result = callPythonOcrApi(upFile);
|
|
|
+ return success(result);
|
|
|
+ } catch (IOException e) {
|
|
|
return error(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
- * 多文件上传
|
|
|
+ * 文字识别
|
|
|
*
|
|
|
- * @param files
|
|
|
+ * @param file 文件
|
|
|
* @return
|
|
|
*/
|
|
|
- @PostMapping("/uploadFiles")
|
|
|
- public AjaxResult uploadFile(@RequestParam(value = "files") List<MultipartFile> files) {
|
|
|
+ @ApiOperation("文字识别-上传图片")
|
|
|
+ @PostMapping("/upload")
|
|
|
+ public AjaxResult ocr(@RequestParam(value = "file") MultipartFile file) {
|
|
|
try {
|
|
|
- return success(mongoService.uploadFiles(files));
|
|
|
+ //选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()方法 。
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ String[] filename = originalFilename.split("\\.");
|
|
|
+ File upFile = File.createTempFile(filename[0], filename[1] + ".");
|
|
|
+ file.transferTo(upFile);
|
|
|
+ String result = callPythonOcrApi(upFile);
|
|
|
+ return success(result);
|
|
|
} catch (Exception e) {
|
|
|
return error(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
- * 文件删除
|
|
|
+ * 调用python接口获取结果
|
|
|
*
|
|
|
- * @param fileId
|
|
|
- * @return
|
|
|
+ * @param file 文件
|
|
|
+ * @return 识别结果
|
|
|
*/
|
|
|
- @DeleteMapping("/remove/{fileId}")
|
|
|
- public AjaxResult removeFile(@PathVariable(name = "fileId") String fileId) {
|
|
|
- mongoService.removeFile(fileId);
|
|
|
- return success("删除成功");
|
|
|
+ private String callPythonOcrApi(File file) {
|
|
|
+ HashMap<String, Object> map = new HashMap<>(3);
|
|
|
+ map.put("file", file);
|
|
|
+ String uri = configService.selectConfigByKey("api.ocr.uri");
|
|
|
+ String result = HttpUtil.post(uri, map);
|
|
|
+ try {
|
|
|
+ Files.delete(file.toPath());
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
+ return result;
|
|
|
}
|
|
|
-
|
|
|
}
|