ocr识别-下载修改

This commit is contained in:
liyawei
2022-03-08 14:58:27 +08:00
parent 39106032e8
commit 4f64a1d0a3
@@ -2,6 +2,7 @@ package com.jero.modules.ocr.controller;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.jero.common.api.vo.Result; import com.jero.common.api.vo.Result;
import com.jero.common.exception.JeroBootException;
import com.jero.modules.ocr.entity.OcrRecordEO; import com.jero.modules.ocr.entity.OcrRecordEO;
import com.jero.modules.ocr.enums.ResultContentEnum; import com.jero.modules.ocr.enums.ResultContentEnum;
import com.jero.modules.ocr.service.IOcrRecordEOService; import com.jero.modules.ocr.service.IOcrRecordEOService;
@@ -12,6 +13,7 @@ import com.jero.modules.oss.service.IOSSFileService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -20,8 +22,10 @@ import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream; import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
@@ -53,9 +57,61 @@ public class OcrRestfulController{
@Value("${OCR.ocrDownPath}") @Value("${OCR.ocrDownPath}")
private String ocrDownPath; private String ocrDownPath;
@Value("${OCR.ocrPath}")
private String ocrPath;
@Value("${jero.path.upload}") @Value("${jero.path.upload}")
private String filePath;//文件存储路径 private String filePath;//文件存储路径
@ApiOperation(value = "下载word文件")
@GetMapping("/downFile")
public void downFile(String fileName, HttpServletResponse response, HttpServletRequest request) throws Exception {
InputStream is = null;
OutputStream os = null;
response.reset();
try {
String fileOldName = fileNameEncoding(fileName,request);
response.setHeader("Content-Disposition", "attachment; filename=" + fileOldName);
response.setContentType("application/octet-stream");
try {
String fullPath = ocrPath + fileOldName;
is = new FileInputStream(new File(fullPath));
} catch (FileNotFoundException var4) {
throw new JeroBootException("文件[" + ocrPath + fileOldName + "]不存在");
}
os = response.getOutputStream();
IOUtils.copy(is, os);
os.flush();
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
is.close();
os.close();
}
}
/**
* @Author yangxuenan
* @Description 根据不同浏览器定义下载文件编码
* Date 2018/10/11 10:40
* @Param [fileName, request]
* @return java.lang.String
**/
public String fileNameEncoding(String fileName, HttpServletRequest request) throws IOException {
String agent = request.getHeader("User-Agent");
if (agent.contains("Firefox")) {
/*BASE64Encoder base64Encoder = new BASE64Encoder();
fileName = "=?utf-8?B?"
+ base64Encoder.encode(fileName.getBytes("utf-8")) + "?=";*/
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1"); // firefox浏览器
} else {
fileName = URLEncoder.encode(fileName, "utf-8");
//谷歌中空格变为+问题
fileName = fileName.replaceAll("\\+","%20");
}
return fileName;
}
/** /**
* 测试OCR * 测试OCR
* @param file * @param file