ocr,拆分文件切换cos

This commit is contained in:
liyawei
2022-06-10 09:42:56 +08:00
parent 8214b38b07
commit 8185b9af62
8 changed files with 126 additions and 57 deletions
@@ -1,6 +1,7 @@
package com.jero.modules.oss.controller;
import com.jero.common.exception.JeroBootException;
import com.jero.common.util.oss.CosBootUtil;
import com.jero.modules.oss.SplitFileUtils;
import com.jero.modules.oss.service.IOSSFileService;
import javax.servlet.http.HttpServletRequest;
@@ -14,11 +15,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Decoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.*;
import java.util.Base64;
@Slf4j
@@ -52,13 +49,15 @@ public class SplitFileController {
"attachment; filename=\""+ SplitFileUtils.encodeFileName(fileName, request) +"\"");
response.setContentType("application/force-download");
try(OutputStream os = response.getOutputStream();
FileInputStream fis = new FileInputStream(fileUrl);) {
int len = 0;
while ((len = fis.read()) != -1) {
os.write(len);
/*FileInputStream fis = new FileInputStream(fileUrl);*/) {
if (CosBootUtil.doesObjectExist(fileUrl)) {
FileInputStream fis = (FileInputStream) CosBootUtil.download(fileUrl);
int len = 0;
while ((len = fis.read()) != -1) {
os.write(len);
}
os.flush();
}
os.flush();
} catch (FileNotFoundException e) {
throw new JeroBootException("文件不存在");
} catch (IOException e) {
@@ -19,9 +19,11 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
@@ -42,8 +44,8 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
private String[] fileSuffixLimits;
// 文档拆分条款图片访问路径
@Value(value = "${jero.path.img}")
private String imgpath;
@Value(value = "${jero.splitUrl}")
private String splitUrl;
@Override
public void upload(MultipartFile multipartFile) throws IOException {
@@ -290,10 +292,7 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
String ctxPath = uploadCospath;
String fileName = null;
String fileType = null;
File file = new File(ctxPath + File.separator + bizPath + File.separator);
if (!file.exists()) {
file.mkdirs();// 创建文件根目录
}
String orgName = mf.getOriginalFilename();// 获取文件名
orgName = CommonUtils.getFileName(orgName);
if (orgName.indexOf(".") != -1) {
@@ -302,6 +301,7 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
fileName = orgName + "_" + System.currentTimeMillis();
}
String filePath = ctxPath + "/" + fileName;
fileType = orgName.substring(orgName.lastIndexOf("."));
String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF";
//判断文件类型
@@ -344,32 +344,24 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
}
}
String savePath = file.getPath() + File.separator + fileName;
File savefile = new File(savePath);
FileCopyUtils.copy(mf.getBytes(), savefile);
String dbpath = null;
if (oConvertUtils.isNotEmpty(bizPath)) {
dbpath = bizPath + File.separator + fileName;
} else {
dbpath = fileName;
}
if (dbpath.contains("\\")) {
dbpath = dbpath.replace("\\", "/");
}
// 将文件上传至腾讯云 cos
CosBootUtil.upload(mf, filePath);
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//存入文件表
oSSFile.setFileName(orgName);
oSSFile.setId(UuidUtils.getUUID());
oSSFile.setUrl(savePath);
oSSFile.setUrl(filePath);
oSSFile.setCreateBy(loginUser.getUsername());
oSSFile.setCreateTime(new Date());
this.save(oSSFile);
// 文件路径做特殊处理
String imgUrl = imgpath + savePath.substring(savePath.lastIndexOf(File.separator)+1);
BASE64Encoder base64Encoder = new BASE64Encoder();
String encode = base64Encoder.encode(filePath.getBytes(StandardCharsets.UTF_8));
String imgUrl = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + encode;
oSSFile.setUrl(imgUrl);
return oSSFile;
} catch (IOException e) {
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return oSSFile;