ApiController.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package com.doc.biz.controller;
  2. import cn.hutool.http.HttpUtil;
  3. import com.doc.biz.service.IMongoService;
  4. import com.doc.biz.vo.DocumentVO;
  5. import com.doc.common.core.controller.BaseController;
  6. import com.doc.common.core.domain.AjaxResult;
  7. import com.doc.system.service.ISysConfigService;
  8. import io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiOperation;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.springframework.http.HttpHeaders;
  12. import org.springframework.http.HttpStatus;
  13. import org.springframework.http.ResponseEntity;
  14. import org.springframework.web.bind.annotation.*;
  15. import org.springframework.web.multipart.MultipartFile;
  16. import org.yaml.snakeyaml.util.UriEncoder;
  17. import javax.annotation.Resource;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.nio.file.Files;
  21. import java.util.HashMap;
  22. import java.util.Objects;
  23. /**
  24. * 文件上传
  25. *
  26. * @author wukai
  27. * @date 2023-08-15
  28. */
  29. @Slf4j
  30. @Api(tags = "通用接口")
  31. @RestController
  32. @RequestMapping("/api")
  33. public class ApiController extends BaseController {
  34. /**
  35. * 文件上传实现类
  36. */
  37. @Resource
  38. private IMongoService mongoService;
  39. @Resource
  40. private ISysConfigService configService;
  41. /**
  42. * 文件下载
  43. *
  44. * @param fileId fileId
  45. * @return
  46. */
  47. @ApiOperation("文件预览")
  48. @GetMapping("/access/{fileId}")
  49. public ResponseEntity<Object> access(@PathVariable(name = "fileId") String fileId) {
  50. return down(fileId, false);
  51. }
  52. /**
  53. * 文件下载
  54. *
  55. * @param fileId fileId
  56. * @return
  57. */
  58. @ApiOperation("文件下载")
  59. @GetMapping("/download/{fileId}")
  60. public ResponseEntity<Object> download(@PathVariable(name = "fileId") String fileId) {
  61. return down(fileId, true);
  62. }
  63. private ResponseEntity<Object> down(String fileId, boolean down) {
  64. DocumentVO vo = mongoService.downloadFile(fileId);
  65. if (Objects.nonNull(vo)) {
  66. String disposition = down ? "attachment" : "inline";
  67. disposition += "; filename=\"" + UriEncoder.encode(vo.getFileName()) + "\"";
  68. return ResponseEntity.ok()
  69. .header(HttpHeaders.CONTENT_DISPOSITION, disposition)
  70. .header(HttpHeaders.CONTENT_TYPE, vo.getContentType())
  71. .header(HttpHeaders.CONTENT_LENGTH, vo.getFileSize() + "")
  72. .header("Connection", "close")
  73. .body(vo.getData());
  74. } else {
  75. return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file does not exist");
  76. }
  77. }
  78. /**
  79. * 文字识别
  80. *
  81. * @param fileId fileId
  82. * @return
  83. */
  84. @ApiOperation("文字识别-通过fileId")
  85. @GetMapping("/ocr/{fileId}")
  86. public AjaxResult ocr(@PathVariable(name = "fileId") String fileId) {
  87. DocumentVO vo = mongoService.downloadFile(fileId);
  88. try {
  89. File upFile = File.createTempFile(vo.getFileName(), vo.getSuffix() + ".");
  90. Files.write(upFile.toPath(), vo.getData());
  91. String result = callPythonOcrApi(upFile);
  92. return success(result);
  93. } catch (IOException e) {
  94. return error(e.getMessage());
  95. }
  96. }
  97. /**
  98. * 文字识别
  99. *
  100. * @param file 文件
  101. * @return
  102. */
  103. @ApiOperation("文字识别-上传图片")
  104. @PostMapping("/upload")
  105. public AjaxResult ocr(@RequestParam(value = "file") MultipartFile file) {
  106. try {
  107. //选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()方法 。
  108. String originalFilename = file.getOriginalFilename();
  109. String[] filename = originalFilename.split("\\.");
  110. File upFile = File.createTempFile(filename[0], filename[1] + ".");
  111. file.transferTo(upFile);
  112. String result = callPythonOcrApi(upFile);
  113. return success(result);
  114. } catch (Exception e) {
  115. return error(e.getMessage());
  116. }
  117. }
  118. /**
  119. * 调用python接口获取结果
  120. *
  121. * @param file 文件
  122. * @return 识别结果
  123. */
  124. private String callPythonOcrApi(File file) {
  125. HashMap<String, Object> map = new HashMap<>(3);
  126. map.put("file", file);
  127. String uri = configService.selectConfigByKey("api.ocr.uri");
  128. String result = HttpUtil.post(uri, map);
  129. try {
  130. Files.delete(file.toPath());
  131. } catch (IOException e) {
  132. }
  133. return result;
  134. }
  135. }