ocr识别-下载修改

This commit is contained in:
liyawei
2022-03-08 15:26:53 +08:00
parent b3935f3e69
commit 6c2ffe8995
@@ -25,7 +25,6 @@ import org.springframework.web.multipart.MultipartFile;
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;
@@ -70,18 +69,16 @@ public class OcrRestfulController{
OutputStream os = null;
response.reset();
try {
String fileOldName = fileNameEncoding(fileName,request);
response.setHeader("Content-Disposition", "attachment; filename=" + fileOldName);
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
response.setContentType("application/octet-stream");
try {
String fullPath = ocrPath + fileOldName;
is = new FileInputStream(new File(fullPath));
} catch (FileNotFoundException var4) {
throw new JeroBootException("文件[" + ocrPath + fileOldName + "]不存在");
}
String fullPath = ocrPath + fileName;
is = new FileInputStream(fullPath);
os = response.getOutputStream();
IOUtils.copy(is, os);
os.flush();
} catch (FileNotFoundException var4) {
throw new JeroBootException("文件[" + ocrPath + fileName + "]不存在");
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
@@ -90,28 +87,6 @@ public class OcrRestfulController{
}
}
/**
* @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