| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- 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.http.HttpHeaders;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.*;
- 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.Objects;
- /**
- * 文件上传
- *
- * @author wukai
- * @date 2023-08-15
- */
- @Slf4j
- @Api(tags = "通用接口")
- @RestController
- @RequestMapping("/api")
- public class ApiController extends BaseController {
- /**
- * 文件上传实现类
- */
- @Resource
- private IMongoService mongoService;
- @Resource
- private ISysConfigService configService;
- /**
- * 文件下载
- *
- * @param fileId fileId
- * @return
- */
- @ApiOperation("文件预览")
- @GetMapping("/access/{fileId}")
- public ResponseEntity<Object> access(@PathVariable(name = "fileId") String fileId) {
- return down(fileId, false);
- }
- /**
- * 文件下载
- *
- * @param fileId fileId
- * @return
- */
- @ApiOperation("文件下载")
- @GetMapping("/download/{fileId}")
- public ResponseEntity<Object> download(@PathVariable(name = "fileId") String fileId) {
- return down(fileId, true);
- }
- private ResponseEntity<Object> down(String fileId, boolean down) {
- DocumentVO vo = mongoService.downloadFile(fileId);
- if (Objects.nonNull(vo)) {
- String disposition = down ? "attachment" : "inline";
- disposition += "; filename=\"" + UriEncoder.encode(vo.getFileName()) + "\"";
- return ResponseEntity.ok()
- .header(HttpHeaders.CONTENT_DISPOSITION, disposition)
- .header(HttpHeaders.CONTENT_TYPE, vo.getContentType())
- .header(HttpHeaders.CONTENT_LENGTH, vo.getFileSize() + "")
- .header("Connection", "close")
- .body(vo.getData());
- } else {
- return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file does not exist");
- }
- }
- /**
- * 文字识别
- *
- * @param fileId fileId
- * @return
- */
- @ApiOperation("文字识别-通过fileId")
- @GetMapping("/ocr/{fileId}")
- public AjaxResult ocr(@PathVariable(name = "fileId") String fileId) {
- DocumentVO vo = mongoService.downloadFile(fileId);
- try {
- 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 file 文件
- * @return
- */
- @ApiOperation("文字识别-上传图片")
- @PostMapping("/upload")
- public AjaxResult ocr(@RequestParam(value = "file") MultipartFile file) {
- try {
- //选择用缓冲区来实现这个转换即使用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 file 文件
- * @return 识别结果
- */
- 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;
- }
- }
|