From f1ab8f1db29bd3bd7d732b43040eba2d74bfee3b Mon Sep 17 00:00:00 2001 From: wangzhijiang <1358525938@qq.com> Date: Tue, 19 Mar 2024 14:34:47 +0800 Subject: [PATCH] =?UTF-8?q?minio=E5=AD=98=E5=82=A8=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E4=BF=AE=E6=94=B9=E4=B8=BAlocal=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jero/common/util/CommonUtils.java | 2 +- .../oss/controller/SplitFileController.java | 47 ++++-- .../service/impl/SysCategoryServiceImpl.java | 6 + .../split/service/impl/FileSpiltService.java | 155 +++++++++++++++--- .../impl/FileSplitItemsEOServiceImpl.java | 47 +++++- .../src/main/resources/application-dev.yml | 4 +- .../src/main/resources/application-prod.yml | 4 +- .../src/main/resources/application-test.yml | 4 +- 8 files changed, 220 insertions(+), 49 deletions(-) diff --git a/byd-structuredplugins-serve/laws-base/laws-base-core/src/main/java/com/jero/common/util/CommonUtils.java b/byd-structuredplugins-serve/laws-base/laws-base-core/src/main/java/com/jero/common/util/CommonUtils.java index a6287e0..db82913 100644 --- a/byd-structuredplugins-serve/laws-base/laws-base-core/src/main/java/com/jero/common/util/CommonUtils.java +++ b/byd-structuredplugins-serve/laws-base/laws-base-core/src/main/java/com/jero/common/util/CommonUtils.java @@ -164,7 +164,7 @@ public class CommonUtils { if (dbpath.contains("\\")) { dbpath = dbpath.replace("\\", "/"); } - return dbpath; + return savePath; } catch (IOException e) { log.error(e.getMessage(), e); } catch (Exception e) { diff --git a/byd-structuredplugins-serve/laws-module-system/src/main/java/com/jero/modules/oss/controller/SplitFileController.java b/byd-structuredplugins-serve/laws-module-system/src/main/java/com/jero/modules/oss/controller/SplitFileController.java index 80d9f7c..9c241ae 100644 --- a/byd-structuredplugins-serve/laws-module-system/src/main/java/com/jero/modules/oss/controller/SplitFileController.java +++ b/byd-structuredplugins-serve/laws-module-system/src/main/java/com/jero/modules/oss/controller/SplitFileController.java @@ -1,7 +1,9 @@ package com.jero.modules.oss.controller; +import com.jero.common.constant.CommonConstant; import com.jero.common.exception.JeroBootException; import com.jero.common.util.MinioUtil; +import com.jero.modules.oss.entity.OSSFile; import com.jero.modules.oss.service.IOSSFileService; import com.jero.modules.oss.util.SplitFileUtils; import lombok.extern.slf4j.Slf4j; @@ -15,10 +17,8 @@ import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; +import java.util.List; @Slf4j @Controller @@ -28,27 +28,50 @@ public class SplitFileController { private String filePathCos; @Autowired private IOSSFileService ossFileService; + @Value("${jero.uploadType}") + private String uploadType; @ResponseBody @GetMapping("/getImage") public void queryPageList(@RequestParam("fileName") String fileName, HttpServletResponse response, HttpServletRequest request) { - String fileUrl = filePathCos + "/" + fileName; - response.setHeader("Content-Disposition", - "attachment; filename=\"" + SplitFileUtils.encodeFileName(fileName, request) + "\""); - response.setContentType("application/force-download"); - if (MinioUtil.doesObjectExist(fileUrl)) { - try (OutputStream os = response.getOutputStream(); - InputStream fis = MinioUtil.download(fileUrl)) { + if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){ + String fileId = fileName; + OSSFile ossFile = ossFileService.getById(fileId); + String fileUrl = ossFile.getUrl(); + File file = new File(fileUrl); + try { + OutputStream os = response.getOutputStream(); + FileInputStream fis = new FileInputStream(file); + int len = 0; while ((len = fis.read()) != -1) { os.write(len); } os.flush(); - } catch (FileNotFoundException e) { + }catch (FileNotFoundException e) { throw new JeroBootException("文件不存在"); } catch (IOException e) { throw new JeroBootException("文件不存在"); } + } else if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) { + String fileUrl = filePathCos + "/" + fileName; + response.setHeader("Content-Disposition", + "attachment; filename=\"" + SplitFileUtils.encodeFileName(fileName, request) + "\""); + response.setContentType("application/force-download"); + if (MinioUtil.doesObjectExist(fileUrl)) { + try (OutputStream os = response.getOutputStream(); + InputStream fis = MinioUtil.download(fileUrl)) { + int len = 0; + while ((len = fis.read()) != -1) { + os.write(len); + } + os.flush(); + } catch (FileNotFoundException e) { + throw new JeroBootException("文件不存在"); + } catch (IOException e) { + throw new JeroBootException("文件不存在"); + } + } } } diff --git a/byd-structuredplugins-serve/laws-module-system/src/main/java/com/jero/modules/system/service/impl/SysCategoryServiceImpl.java b/byd-structuredplugins-serve/laws-module-system/src/main/java/com/jero/modules/system/service/impl/SysCategoryServiceImpl.java index 80978ef..0aee283 100644 --- a/byd-structuredplugins-serve/laws-module-system/src/main/java/com/jero/modules/system/service/impl/SysCategoryServiceImpl.java +++ b/byd-structuredplugins-serve/laws-module-system/src/main/java/com/jero/modules/system/service/impl/SysCategoryServiceImpl.java @@ -429,4 +429,10 @@ public class SysCategoryServiceImpl extends ServiceImpl list = list(queryWrapper); return list; } + + public List getList(){ +// List list = this.list(); + List list = new ArrayList<>(); + return list; + } } diff --git a/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java b/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java index f1bd693..5e951f5 100644 --- a/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java +++ b/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java @@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.jero.common.constant.CommonConstant; import com.jero.common.exception.JeroBootException; +import com.jero.common.util.CommonUtils; +import com.jero.common.util.DateUtils; import com.jero.common.util.MinioUtil; import com.jero.common.util.UUIDUtils; import com.jero.modules.oss.entity.OSSFile; @@ -21,6 +23,7 @@ import com.jero.modules.split.util.ReadWordTable; import com.jero.modules.split.vo.TitleNumberVO; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; @@ -118,6 +121,17 @@ public class FileSpiltService { @Value("${byd.downLoadFileUrl}") private String bydDownLoadFileUrl; + /** + * 本地:local minio:minio 阿里:alioss + */ + @Value(value = "${jero.uploadType}") + private String uploadType; + /** + * 拆分图片文件路径 + */ + @Value(value = "${jero.path.splitImgPath}") + private String splitImgPath; + /** * 中国标准数据拆分 * @@ -187,10 +201,19 @@ public class FileSpiltService { } OSSFile ossFile = ossFileList.get(0); String readFilePath = ossFile.getUrl(); - if (StringUtils.isEmpty(readFilePath) || !MinioUtil.doesObjectExist(readFilePath)) { - throw new JeroBootException("当前文件未找到,请重新选择!"); + if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) { + if (StringUtils.isEmpty(readFilePath) || !MinioUtil.doesObjectExist(readFilePath)) { + throw new JeroBootException("当前文件未找到,请重新选择!"); + } + splitFileIs = MinioUtil.download(readFilePath); + } else if (CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){ + try { + splitFileIs = new FileInputStream(readFilePath); + } catch (FileNotFoundException e) { + e.printStackTrace(); + throw new JeroBootException("当前文件未找到,请重新选择!"); + } } - splitFileIs = MinioUtil.download(readFilePath); } else { log.info("下载文档开始========================================================="); try { @@ -276,7 +299,7 @@ public class FileSpiltService { // addItermsMathImage(messageList.get(messageList.size() - 1), itemValList, imageName); //} File mathToImgFile = this.getMathToImgUrl(ctoMath.xmlText()); - if (messageList.size() > 0) { + if (null != mathToImgFile && messageList.size() > 0) { addItermsMathImage(messageList.get(messageList.size() - 1), itemValList, mathToImgFile); } } else if (child instanceof CTOMathPara) { @@ -287,7 +310,7 @@ public class FileSpiltService { //} File mathToImgFile = this.getMathToImgUrl(ctoMathPara.xmlText()); - if (messageList.size() > 0) { + if (null != mathToImgFile && messageList.size() > 0) { addItermsMathImage(messageList.get(messageList.size() - 1), itemValList, mathToImgFile); } } @@ -1204,18 +1227,42 @@ public class FileSpiltService { // 像每个条款中依次插入每一段的内容 private void addItermsMathImage(SarFileSplitItemsEO message, List itemsValList, File file) { - byte[] bytev = null; - try (FileInputStream fis = new FileInputStream(file)){ - bytev = new byte[(int) file.length()]; - fis.read(bytev); - }catch (IOException e){ - e.printStackTrace(); - } - String imageName = UUIDUtils.randomUUID10() + file.getName(); String path = splitUrl + imageName; - String filepathandname = filePathCos + "/" + imageName; - MinioUtil.uploadByte(bytev, filepathandname); + if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){ + File splitImgFolder = new File(splitImgPath); + if (!splitImgFolder.exists()) { + splitImgFolder.mkdirs(); + } + + String newFilePath = splitImgPath + imageName; + try { + File newFile = new File(newFilePath); + FileUtils.copyFile(file, newFile); + } catch (IOException e) { + e.printStackTrace(); + } + + //上传成功 进行数据库存储 + OSSFile ossFile = new OSSFile(); + // 文件名 + String fileName = file.getName(); + fileName = CommonUtils.getFileName(fileName); + ossFile.setFileName(fileName); + ossFile.setUrl(newFilePath); + ossFile.setId(imageName); + iossFileService.save(ossFile); + } else if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){ + byte[] bytev = null; + try (FileInputStream fis = new FileInputStream(file)){ + bytev = new byte[(int) file.length()]; + fis.read(bytev); + }catch (IOException e){ + e.printStackTrace(); + } + String filepathandname = filePathCos + "/" + imageName; + MinioUtil.uploadByte(bytev, filepathandname); + } String imgConVal = ""; message.getItemsCondi().add(imgConVal); @@ -1228,13 +1275,42 @@ public class FileSpiltService { for (String pictureId : imageBundleList) { XWPFPictureData pictureData = p.getDocument().getPictureDataByID(pictureId); String imageName = UUIDUtils.randomUUID10() + pictureData.getFileName(); + String path = splitUrl + imageName; + byte[] bytev = pictureData.getData(); - String filepathandname = filePathCos + "/" + imageName; - try { - String path = splitUrl+imageName; + String imgConVal = ""; + String imgCon = ""; + if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){ + String fileName = pictureData.getFileName(); + File splitImgFolder = new File(splitImgPath); + if (!splitImgFolder.exists()) { + splitImgFolder.mkdirs(); + } + + String newFilePath = splitImgPath + imageName; + try (FileOutputStream fos = new FileOutputStream(newFilePath)) { + fos.write(bytev); + } catch (IOException e) { + e.printStackTrace(); + log.error("写入图片时发生错误:" + e.getMessage()); + } + + //上传成功 进行数据库存储 + OSSFile ossFile = new OSSFile(); + // 文件名 + fileName = CommonUtils.getFileName(fileName); + ossFile.setFileName(fileName); + ossFile.setUrl(newFilePath); + ossFile.setId(imageName); + iossFileService.save(ossFile); + } else if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) { + + String filepathandname = filePathCos + "/" + imageName; MinioUtil.uploadByte(bytev, filepathandname); - String imgCon = path; - String imgConVal = ""; + } + imgCon = path; + imgConVal = ""; + try { message.getItemsCondi().add(imgConVal); message.setItemContent(message.getItemContent() + imgConVal); itemsValList.add(getSplitItemsValObject(message.getId(), SplitFilePragraTypeEnum.IMG.getValue(), imgCon, message.getItemsCondi().size(), null)); @@ -1250,12 +1326,39 @@ public class FileSpiltService { XWPFPictureData pictureData = p.getDocument().getPictureDataByID(pictureId); String imageName = UUIDUtils.randomUUID10() + pictureData.getFileName(); byte[] bytev = pictureData.getData(); - String filepathandname = filePathCos + "/" + imageName; - try { - String path = splitUrl+imageName; + String path = splitUrl+imageName; + if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){ + String fileName = pictureData.getFileName(); + + File splitImgFolder = new File(splitImgPath); + if (!splitImgFolder.exists()) { + splitImgFolder.mkdirs(); + } + + String newFilePath = splitImgPath + imageName; + try (FileOutputStream fos = new FileOutputStream(newFilePath)) { + fos.write(bytev); + } catch (IOException e) { + e.printStackTrace(); + log.error("写入图片时发生错误:" + e.getMessage()); + } + + //上传成功 进行数据库存储 + OSSFile ossFile = new OSSFile(); + // 文件名 + fileName = CommonUtils.getFileName(fileName); + ossFile.setFileName(fileName); + ossFile.setUrl(newFilePath); + ossFile.setId(imageName); + iossFileService.save(ossFile); + + } else if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) { + String filepathandname = filePathCos + "/" + imageName; MinioUtil.uploadByte(bytev, filepathandname); + } + try { String imgCon = path; - String imgConVal = ""; + String imgConVal = ""; stringBuilder.append(imgConVal); } catch (Exception e) { e.printStackTrace(); @@ -1374,7 +1477,9 @@ public class FileSpiltService { HttpResponse res = client.execute(post); String results = EntityUtils.toString(res.getEntity()); - result = new File(results); + if (StringUtils.isNotEmpty(results)) { + result = new File(results); + } } catch (Exception e) { e.printStackTrace(); } diff --git a/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSplitItemsEOServiceImpl.java b/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSplitItemsEOServiceImpl.java index 55d2cfc..4e25c29 100644 --- a/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSplitItemsEOServiceImpl.java +++ b/byd-structuredplugins-serve/laws-modules/src/main/java/com/jero/modules/split/service/impl/FileSplitItemsEOServiceImpl.java @@ -11,6 +11,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.jero.common.api.vo.Result; +import com.jero.common.constant.CommonConstant; import com.jero.common.constant.enums.LanguageEnum; import com.jero.common.constant.enums.ModuleEnum; import com.jero.common.constant.enums.YesOrNoEnum; @@ -139,6 +140,13 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl parameter){ @@ -556,7 +564,7 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl categoryList = sysCategoryService.list(); + List categoryList = sysCategoryService.getList(); //过滤出树形字段 List treeFieldList = fieldList.stream() .filter(e -> FieldTypeEnum.TREE.getValue().equals(e.getFieldShowType())) @@ -3278,7 +3286,7 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl categoryList = sysCategoryService.list(); + List categoryList = sysCategoryService.getList(); //过滤出树形字段 List treeFieldList = fieldList.stream() .filter(e -> FieldTypeEnum.TREE.getValue().equals(e.getFieldShowType())) @@ -3459,15 +3467,27 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl allImgList, String fileNowPath) { for(FileSplitValImgExportDto imgExportDto : allImgList){ - String oldPath = uploadCospath + "/" + imgExportDto.getImgPath(); + String newPath = fileNowPath + File.separator + imgExportDto.getImgName(); - if (MinioUtil.doesObjectExist(oldPath)){ - try (InputStream in = MinioUtil.download(oldPath);) { + if (CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)) { + String oldPath = splitImgPath + "/" + imgExportDto.getImgPath(); + try { + InputStream in = new FileInputStream(oldPath); copyFile1(in, newPath); } catch (IOException e) { e.printStackTrace(); log.error(e.getMessage(), e); } + } else if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) { + String oldPath = uploadCospath + "/" + imgExportDto.getImgPath(); + if (MinioUtil.doesObjectExist(oldPath)){ + try (InputStream in = MinioUtil.download(oldPath);) { + copyFile1(in, newPath); + } catch (IOException e) { + e.printStackTrace(); + log.error(e.getMessage(), e); + } + } } } @@ -3476,15 +3496,26 @@ public class FileSplitItemsEOServiceImpl extends ServiceImpl allRelevFileList, String fileNowPath) { for(OSSFile fileExportDto : allRelevFileList){ - String oldPath = fileExportDto.getUrl(); String newPath = fileNowPath + File.separator + fileExportDto.getFileName(); - if (MinioUtil.doesObjectExist(oldPath)){ - try (InputStream in = MinioUtil.download(oldPath);) { + String oldPath = fileExportDto.getUrl(); + + if (CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)) { + try { + InputStream in = new FileInputStream(oldPath); copyFile1(in, newPath); } catch (IOException e) { e.printStackTrace(); log.error(e.getMessage(), e); } + } else if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) { + if (MinioUtil.doesObjectExist(oldPath)){ + try (InputStream in = MinioUtil.download(oldPath);) { + copyFile1(in, newPath); + } catch (IOException e) { + e.printStackTrace(); + log.error(e.getMessage(), e); + } + } } } diff --git a/byd-structuredplugins-serve/laws-single-startup/src/main/resources/application-dev.yml b/byd-structuredplugins-serve/laws-single-startup/src/main/resources/application-dev.yml index 6281ae7..44f9490 100644 --- a/byd-structuredplugins-serve/laws-single-startup/src/main/resources/application-dev.yml +++ b/byd-structuredplugins-serve/laws-single-startup/src/main/resources/application-dev.yml @@ -132,7 +132,7 @@ jero: # 签名密钥串(前后端要一致,正式发布请自行修改) signatureSecret: dd05f1c54d63749eda95f9fa6d49v442a # 本地:local\Minio:minio\阿里云:alioss - uploadType: minio + uploadType: local #拆分图片展示地址 splitUrl: http://localhost:8184/byd-structuredplugins-serve/sys/split/file/getImage?fileName= path: @@ -148,6 +148,8 @@ jero: exportExcelTempPath: D://opt//exportExcelTemp #仿宋体字体文件路径 设置 simfangFontFilePath: D://opt//simfangFontFilePath//simfang.ttf + #拆分图片文件路径 设置 + splitImgPath: D://opt//splitImgFiles/ shiro: excludeUrls: /test/jeroDemo/demo3,/test/jeroDemo/redisDemo/**,/category/**,/visual/**,/map/**,/jmreport/bigscreen2/** #阿里云oss存储配置 diff --git a/byd-structuredplugins-serve/laws-single-startup/src/main/resources/application-prod.yml b/byd-structuredplugins-serve/laws-single-startup/src/main/resources/application-prod.yml index 86b8f55..93e5a4f 100644 --- a/byd-structuredplugins-serve/laws-single-startup/src/main/resources/application-prod.yml +++ b/byd-structuredplugins-serve/laws-single-startup/src/main/resources/application-prod.yml @@ -122,7 +122,7 @@ jero: # 签名密钥串(前后端要一致,正式发布请自行修改) signatureSecret: dd05f1c54d63749eda95f9fa6d49v442a # 本地:local\Minio:minio\阿里云:alioss - uploadType: minio + uploadType: local #拆分图片展示地址 splitUrl: http://srms.sinotruk.com/byd-structuredplugins-serve/sys/split/file/getImage?fileName= path: @@ -137,6 +137,8 @@ jero: exportExcelTempPath: D://opt//exportExcelTemp #仿宋体字体文件路径 设置 simfangFontFilePath: D://opt//simfangFontFilePath//simfang.ttf + #拆分图片文件路径 设置 + splitImgPath: D://opt//splitImgFiles/ shiro: excludeUrls: /test/jeroDemo/demo3,/test/jeroDemo/redisDemo/**,/category/**,/visual/**,/map/**,/jmreport/bigscreen2/** #阿里云oss存储配置 diff --git a/byd-structuredplugins-serve/laws-single-startup/src/main/resources/application-test.yml b/byd-structuredplugins-serve/laws-single-startup/src/main/resources/application-test.yml index eea7499..6dcc3ba 100644 --- a/byd-structuredplugins-serve/laws-single-startup/src/main/resources/application-test.yml +++ b/byd-structuredplugins-serve/laws-single-startup/src/main/resources/application-test.yml @@ -131,7 +131,7 @@ jero: # 签名密钥串(前后端要一致,正式发布请自行修改) signatureSecret: dd05f1c54d63749eda95f9fa6d49v442a # 本地:local\Minio:minio\阿里云:alioss - uploadType: minio + uploadType: local #拆分图片展示地址 splitUrl: http://39.98.140.126:8186/byd-structuredplugins-serve/sys/split/file/getImage?fileName= path: @@ -147,6 +147,8 @@ jero: exportExcelTempPath: D://opt//exportExcelTemp #仿宋体字体文件路径 设置 simfangFontFilePath: D://opt//simfangFontFilePath//simfang.ttf + #拆分图片文件路径 设置 + splitImgPath: D://opt//splitImgFiles/ shiro: excludeUrls: /test/jeroDemo/demo3,/test/jeroDemo/redisDemo/**,/category/**,/visual/**,/map/**,/jmreport/bigscreen2/** #阿里云oss存储配置