From 4f64a1d0a3dd71d8f6c7b52a0b2e19a70db76fc6 Mon Sep 17 00:00:00 2001 From: liyawei Date: Tue, 8 Mar 2022 14:58:27 +0800 Subject: [PATCH] =?UTF-8?q?ocr=E8=AF=86=E5=88=AB-=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ocr/controller/OcrRestfulController.java | 60 ++++++++++++++++++- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ocr/controller/OcrRestfulController.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ocr/controller/OcrRestfulController.java index e6366b319..52dd7db3c 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ocr/controller/OcrRestfulController.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ocr/controller/OcrRestfulController.java @@ -2,6 +2,7 @@ package com.jero.modules.ocr.controller; import cn.hutool.core.util.ObjectUtil; 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.enums.ResultContentEnum; 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.ApiOperation; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.http.entity.ContentType; 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.multipart.MultipartFile; -import java.io.File; -import java.io.FileInputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; @@ -53,9 +57,61 @@ public class OcrRestfulController{ @Value("${OCR.ocrDownPath}") private String ocrDownPath; + @Value("${OCR.ocrPath}") + private String ocrPath; + @Value("${jero.path.upload}") 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 * @param file