From 5c309fc462e20cb5b932add1da62dd304a977092 Mon Sep 17 00:00:00 2001 From: "1437892690@qq.com" <1437892690@qq.com> Date: Fri, 19 Dec 2025 10:36:38 +0800 Subject: [PATCH 1/5] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]=20=E9=99=84=E4=BB=B6?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E5=B7=A5=E5=85=B7=E7=B1=BBFileUtil=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=8C=87=E5=AE=9A=E7=9B=AE=E6=A0=87=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1579604316225540]附件存储工具类FileUtil支持指定目标路径方式 http://192.168.0.96:8090/demo/rdm.html#/story-detail/939050947543040/939050947543042/1579604316225540 --- .../framework/file/core/IFileStorageHandler.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/neatlogic/framework/file/core/IFileStorageHandler.java b/src/main/java/neatlogic/framework/file/core/IFileStorageHandler.java index 021a0cd3a..8827649bf 100644 --- a/src/main/java/neatlogic/framework/file/core/IFileStorageHandler.java +++ b/src/main/java/neatlogic/framework/file/core/IFileStorageHandler.java @@ -22,7 +22,17 @@ public interface IFileStorageHandler { String saveData(String tenantUuid, InputStream inputStream, FileVo file) throws Exception; - InputStream getData(String path) throws Exception; + /** + * 上传文件到固定路径 + * @param inputStream 流 + * @param contentType 文件类型 + * @param filePath 目标路径 + * @return + * @throws Exception + */ + String saveData(InputStream inputStream, String contentType, String filePath) throws Exception; + + InputStream getData(String filePath) throws Exception; void deleteData(String filePath) throws Exception; -- Gitee From da897800058753451c6a892d1a753b05e4353605 Mon Sep 17 00:00:00 2001 From: "1437892690@qq.com" <1437892690@qq.com> Date: Fri, 19 Dec 2025 10:40:47 +0800 Subject: [PATCH 2/5] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]=20=E9=99=84=E4=BB=B6?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E5=B7=A5=E5=85=B7=E7=B1=BBFileUtil=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=8C=87=E5=AE=9A=E7=9B=AE=E6=A0=87=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1579604316225540]附件存储工具类FileUtil支持指定目标路径方式 http://192.168.0.96:8090/demo/rdm.html#/story-detail/939050947543040/939050947543042/1579604316225540 --- .../file/handler/LocalFileSystemHandler.java | 65 +++++++++++++++---- 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/src/main/java/neatlogic/module/framework/file/handler/LocalFileSystemHandler.java b/src/main/java/neatlogic/module/framework/file/handler/LocalFileSystemHandler.java index 467f15b36..b728ecb9e 100644 --- a/src/main/java/neatlogic/module/framework/file/handler/LocalFileSystemHandler.java +++ b/src/main/java/neatlogic/module/framework/file/handler/LocalFileSystemHandler.java @@ -40,25 +40,53 @@ public class LocalFileSystemHandler implements IFileStorageHandler { @Override public String saveData(String tenantUuid, InputStream inputStream, FileVo fileParam) throws Exception { +// SimpleDateFormat format = new SimpleDateFormat("yyyy" + File.separator + "MM" + File.separator + "dd"); +// String filePath = tenantUuid + File.separator + fileParam.getType() + File.separator + format.format(new Date()) + File.separator + fileParam.getPathName(); +// String finalPath = Config.DATA_HOME() + filePath; +// File file = new File(finalPath); +// if (!file.getParentFile().exists()) { +// file.getParentFile().mkdirs(); +// } +// FileOutputStream fos = new FileOutputStream(file); +// IOUtils.copyLarge(inputStream, fos); +// fos.flush(); +// fos.close(); +//// fileVo.setPath("file:" + filePath); +// return LocalFileSystemHandler.NAME.toLowerCase() + ":" + finalPath; SimpleDateFormat format = new SimpleDateFormat("yyyy" + File.separator + "MM" + File.separator + "dd"); String filePath = tenantUuid + File.separator + fileParam.getType() + File.separator + format.format(new Date()) + File.separator + fileParam.getPathName(); - String finalPath = Config.DATA_HOME() + filePath; - File file = new File(finalPath); + filePath = Config.DATA_HOME() + filePath; + return saveData(inputStream, fileParam.getContentType(), filePath); + } + + /** + * 上传文件到固定路径 + * + * @param inputStream 流 + * @param contentType 文件类型 + * @param filePath 目标路径 + * @return + * @throws Exception + */ + @Override + public String saveData(InputStream inputStream, String contentType, String filePath) throws Exception { + File file = new File(filePath); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } - FileOutputStream fos = new FileOutputStream(file); - IOUtils.copyLarge(inputStream, fos); - fos.flush(); - fos.close(); -// fileVo.setPath("file:" + filePath); - return LocalFileSystemHandler.NAME.toLowerCase() + ":" + finalPath; + try (FileOutputStream fos = new FileOutputStream(file)) { + IOUtils.copyLarge(inputStream, fos); + fos.flush(); + } + return LocalFileSystemHandler.NAME.toLowerCase() + ":" + filePath; } @Override - public InputStream getData(String path) throws Exception { + public InputStream getData(String filePath) throws Exception { InputStream in = null; - File file = new File(path.substring(5)); + filePath = filePath.replaceAll(NAME.toLowerCase() + ":", ""); + filePath = filePath.replaceAll(NAME.toUpperCase() + ":", ""); + File file = new File(filePath); if (file.exists() && file.isFile()) { in = Files.newInputStream(file.toPath()); } @@ -67,7 +95,9 @@ public class LocalFileSystemHandler implements IFileStorageHandler { @Override public void deleteData(String filePath) throws Exception { - if (StringUtils.isNotBlank(filePath) && filePath.startsWith(LocalFileSystemHandler.NAME.toLowerCase() + ":")) { + if (StringUtils.isNotBlank(filePath)) { + filePath = filePath.replaceAll(NAME.toLowerCase() + ":", ""); + filePath = filePath.replaceAll(NAME.toUpperCase() + ":", ""); File file = new File(filePath.substring(5)); if (file.exists()) { file.delete(); @@ -80,7 +110,9 @@ public class LocalFileSystemHandler implements IFileStorageHandler { @Override public long getDataLength(String filePath) { long length = 0; - File file = new File(filePath.substring(5)); + filePath = filePath.replaceAll(NAME.toLowerCase() + ":", ""); + filePath = filePath.replaceAll(NAME.toUpperCase() + ":", ""); + File file = new File(filePath); if (file.exists() && file.isFile()) { length = file.length(); } @@ -89,7 +121,12 @@ public class LocalFileSystemHandler implements IFileStorageHandler { @Override public boolean isExit(String filePath) throws Exception { - File file = new File(filePath.substring(5)); - return file.exists(); + if (StringUtils.isNotBlank(filePath)) { + filePath = filePath.replaceAll(NAME.toLowerCase() + ":", ""); + filePath = filePath.replaceAll(NAME.toUpperCase() + ":", ""); + File file = new File(filePath); + return file.exists(); + } + return false; } } -- Gitee From e1b9922a11d73e8fcb93827baffbcd642f503336 Mon Sep 17 00:00:00 2001 From: "1437892690@qq.com" <1437892690@qq.com> Date: Fri, 19 Dec 2025 10:43:20 +0800 Subject: [PATCH 3/5] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]=20=E9=99=84=E4=BB=B6?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E5=B7=A5=E5=85=B7=E7=B1=BBFileUtil=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=8C=87=E5=AE=9A=E7=9B=AE=E6=A0=87=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1579604316225540]附件存储工具类FileUtil支持指定目标路径方式 http://192.168.0.96:8090/demo/rdm.html#/story-detail/939050947543040/939050947543042/1579604316225540 --- .../file/handler/MinioFileSystemHandler.java | 96 +++++++++++++++---- 1 file changed, 80 insertions(+), 16 deletions(-) diff --git a/src/main/java/neatlogic/module/framework/file/handler/MinioFileSystemHandler.java b/src/main/java/neatlogic/module/framework/file/handler/MinioFileSystemHandler.java index 432e0e089..d42d6abf5 100755 --- a/src/main/java/neatlogic/module/framework/file/handler/MinioFileSystemHandler.java +++ b/src/main/java/neatlogic/module/framework/file/handler/MinioFileSystemHandler.java @@ -62,6 +62,53 @@ public class MinioFileSystemHandler implements InitializingBean, IFileStorageHan */ @Override public String saveData(String tenantUuid, InputStream inputStream, FileVo fileParam) throws Exception { +// if (minioClient == null) { +// throw new FileStorageMediumHandlerNotFoundException("minio"); +// } +// // 检查存储桶是否已经存在 +// String bucket = Config.getConfigProperty("minio.bucket", "neatlogic"); +// //boolean bucketExists = minioClient.bucketExists(Config.getConfigProperty("minio.bucket", "neatlogic")); +// boolean bucketExists = +// minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build()); +// if (!bucketExists) { +// // 创建一个名为bucketName的存储桶,用于存储照片等zip文件。 +// //minioClient.makeBucket(Config.getConfigProperty("minio.bucket", "neatlogic")); +// minioClient.makeBucket( +// MakeBucketArgs.builder().bucket(bucket).build() +// ); +// } +// +// +// SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm"); +// String finalPath = "/" + tenantUuid + "/upload/" + fileParam.getType() + "/" + format.format(new Date()) + "/" + fileParam.getPathName(); +// // 使用putObject上传一个文件到存储桶中 +// //minioClient.putObject(Config.getConfigProperty("minio.bucket", "neatlogic"), finalPath, inputStream, fileParam.getContentType()); +// minioClient.putObject( +// PutObjectArgs.builder() +// .bucket(bucket) +// .object(finalPath) +// .stream(inputStream, inputStream.available(), -1) +// .contentType(fileParam.getContentType()) +// .build()); +//// fileVo.setPath("minio:" + finalPath); +// return MinioFileSystemHandler.NAME.toLowerCase() + ":" + finalPath; + + SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm"); + String finalPath = tenantUuid + "/upload/" + fileParam.getType() + "/" + format.format(new Date()) + "/" + fileParam.getPathName(); + return saveData(inputStream, fileParam.getContentType(), finalPath); + } + + /** + * 上传文件到固定路径 + * + * @param inputStream 流 + * @param contentType 文件类型 + * @param filePath 目标路径 + * @return + * @throws Exception + */ + @Override + public String saveData(InputStream inputStream, String contentType, String filePath) throws Exception { if (minioClient == null) { throw new FileStorageMediumHandlerNotFoundException("minio"); } @@ -77,21 +124,19 @@ public class MinioFileSystemHandler implements InitializingBean, IFileStorageHan MakeBucketArgs.builder().bucket(bucket).build() ); } - - - SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm"); - String finalPath = "/" + tenantUuid + "/upload/" + fileParam.getType() + "/" + format.format(new Date()) + "/" + fileParam.getPathName(); + if (filePath.startsWith("/")) { + filePath = filePath.substring(1); + } // 使用putObject上传一个文件到存储桶中 //minioClient.putObject(Config.getConfigProperty("minio.bucket", "neatlogic"), finalPath, inputStream, fileParam.getContentType()); minioClient.putObject( PutObjectArgs.builder() .bucket(bucket) - .object(finalPath) + .object(filePath) .stream(inputStream, inputStream.available(), -1) - .contentType(fileParam.getContentType()) + .contentType(contentType) .build()); -// fileVo.setPath("minio:" + finalPath); - return MinioFileSystemHandler.NAME.toLowerCase() + ":" + finalPath; + return MinioFileSystemHandler.NAME.toLowerCase() + ":" + filePath; } /** @@ -103,11 +148,15 @@ public class MinioFileSystemHandler implements InitializingBean, IFileStorageHan if (minioClient == null) { throw new FileStorageMediumHandlerNotFoundException("minio"); } - if (StringUtils.isNotBlank(filePath) && filePath.startsWith(NAME.toLowerCase() + ":")) { - String path = filePath.replaceAll(NAME.toLowerCase() + ":", ""); + if (StringUtils.isNotBlank(filePath)) { + filePath = filePath.replaceAll(NAME.toLowerCase() + ":", ""); + filePath = filePath.replaceAll(NAME.toUpperCase() + ":", ""); + if (filePath.startsWith("/")) { + filePath = filePath.substring(1); + } //minioClient.removeObject(Config.getConfigProperty("minio.bucket", "neatlogic"), path); minioClient.removeObject(RemoveObjectArgs.builder().bucket(Config.getConfigProperty("minio.bucket", "neatlogic")) - .object(path).build()); + .object(filePath).build()); } else { throw new FilePathIllegalException(filePath); @@ -118,18 +167,23 @@ public class MinioFileSystemHandler implements InitializingBean, IFileStorageHan /** * 获取附件输入流 * - * @param path 附件路径 + * @param filePath 附件路径 * @return 附件输入流 */ @Override - public InputStream getData(String path) throws Exception { + public InputStream getData(String filePath) throws Exception { if (minioClient == null) { throw new FileStorageMediumHandlerNotFoundException("minio"); } + filePath = filePath.replaceAll(NAME.toLowerCase() + ":", ""); + filePath = filePath.replaceAll(NAME.toUpperCase() + ":", ""); + if (filePath.startsWith("/")) { + filePath = filePath.substring(1); + } //return minioClient.getObject(Config.getConfigProperty("minio.bucket", "neatlogic"), path.replaceAll(NAME.toLowerCase() + ":", "")); return minioClient.getObject(GetObjectArgs.builder() .bucket(Config.getConfigProperty("minio.bucket", "neatlogic")) - .object(path.replaceAll(NAME.toLowerCase() + ":", "")).build() + .object(filePath).build() ); } @@ -138,9 +192,14 @@ public class MinioFileSystemHandler implements InitializingBean, IFileStorageHan if (minioClient == null) { throw new FileStorageMediumHandlerNotFoundException("minio"); } + filePath = filePath.replaceAll(NAME.toLowerCase() + ":", ""); + filePath = filePath.replaceAll(NAME.toUpperCase() + ":", ""); + if (filePath.startsWith("/")) { + filePath = filePath.substring(1); + } //return minioClient.statObject(Config.getConfigProperty("minio.bucket", "neatlogic"), filePath.replaceAll(NAME.toLowerCase() + ":", "")).length(); return minioClient.statObject(StatObjectArgs.builder().bucket(Config.getConfigProperty("minio.bucket", "neatlogic")) - .object(filePath.replaceAll(NAME.toLowerCase() + ":", "")).build()).size(); + .object(filePath).build()).size(); } @Override @@ -148,10 +207,15 @@ public class MinioFileSystemHandler implements InitializingBean, IFileStorageHan if (minioClient == null) { throw new FileStorageMediumHandlerNotFoundException("minio"); } + filePath = filePath.replaceAll(NAME.toLowerCase() + ":", ""); + filePath = filePath.replaceAll(NAME.toUpperCase() + ":", ""); + if (filePath.startsWith("/")) { + filePath = filePath.substring(1); + } //minioClient.statObject(Config.getConfigProperty("minio.bucket", "neatlogic"), filePath.replaceAll(NAME.toLowerCase() + ":", "")); try { minioClient.statObject(StatObjectArgs.builder().bucket(Config.getConfigProperty("minio.bucket", "neatlogic")) - .object(filePath.replaceAll(NAME.toLowerCase() + ":", "")).build()); + .object(filePath).build()); return true; } catch (ErrorResponseException e) { if (e.errorResponse().code().equals("NoSuchKey")) { -- Gitee From 9de03803c29b52f058a84728e4bb3a268aa54f01 Mon Sep 17 00:00:00 2001 From: "1437892690@qq.com" <1437892690@qq.com> Date: Fri, 19 Dec 2025 10:59:26 +0800 Subject: [PATCH 4/5] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]=20=E9=99=84=E4=BB=B6?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E5=B7=A5=E5=85=B7=E7=B1=BBFileUtil=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=8C=87=E5=AE=9A=E7=9B=AE=E6=A0=87=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1579604316225540]附件存储工具类FileUtil支持指定目标路径方式 http://192.168.0.96:8090/demo/rdm.html#/story-detail/939050947543040/939050947543042/1579604316225540 --- .../file/handler/AliossFileSystemHandler.java | 68 ++++++++++++++----- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/src/main/java/neatlogic/module/framework/file/handler/AliossFileSystemHandler.java b/src/main/java/neatlogic/module/framework/file/handler/AliossFileSystemHandler.java index 8681d4492..bd273977f 100644 --- a/src/main/java/neatlogic/module/framework/file/handler/AliossFileSystemHandler.java +++ b/src/main/java/neatlogic/module/framework/file/handler/AliossFileSystemHandler.java @@ -60,6 +60,41 @@ public class AliossFileSystemHandler implements InitializingBean, IFileStorageHa */ @Override public String saveData(String tenantUuid, InputStream inputStream, FileVo file) throws Exception { +// String bucket = Config.getConfigProperty("alioss.bucket", "neatlogic"); +// if (ossClient == null) { +// throw new FileStorageMediumHandlerNotFoundException("alioss"); +// } +// // 检查存储桶是否已经存在 +// boolean bucketExists = ossClient.doesBucketExist(bucket); +// if (!bucketExists) { +// // 创建一个名为bucketName的存储桶,用于存储照片等zip文件。 +// ossClient.createBucket(Config.getConfigProperty("alioss.bucket", "neatlogic")); +// } +// SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm"); +// String finalPath = "/" + tenantUuid + "/upload/" + file.getType() + "/" + format.format(new Date()) + "/" + file.getPathName(); +// // 使用putObject上传一个文件到存储桶中 +// ObjectMetadata metadata = new ObjectMetadata(); +// metadata.setContentType(file.getContentType()); +// ossClient.putObject(bucket, finalPath, inputStream, metadata); +//// fileVo.setPath("minio:" + finalPath); +// return AliossFileSystemHandler.NAME.toLowerCase() + ":" + finalPath; + + SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm"); + String filePath = "/" + tenantUuid + "/upload/" + file.getType() + "/" + format.format(new Date()) + "/" + file.getPathName(); + return saveData(inputStream, file.getContentType(), filePath); + } + + /** + * 上传文件到固定路径 + * + * @param inputStream 流 + * @param contentType 文件类型 + * @param filePath 目标路径 + * @return + * @throws Exception + */ + @Override + public String saveData(InputStream inputStream, String contentType, String filePath) throws Exception { String bucket = Config.getConfigProperty("alioss.bucket", "neatlogic"); if (ossClient == null) { throw new FileStorageMediumHandlerNotFoundException("alioss"); @@ -70,22 +105,21 @@ public class AliossFileSystemHandler implements InitializingBean, IFileStorageHa // 创建一个名为bucketName的存储桶,用于存储照片等zip文件。 ossClient.createBucket(Config.getConfigProperty("alioss.bucket", "neatlogic")); } - SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm"); - String finalPath = "/" + tenantUuid + "/upload/" + file.getType() + "/" + format.format(new Date()) + "/" + file.getPathName(); // 使用putObject上传一个文件到存储桶中 ObjectMetadata metadata = new ObjectMetadata(); - metadata.setContentType(file.getContentType()); - ossClient.putObject(bucket, finalPath, inputStream, metadata); -// fileVo.setPath("minio:" + finalPath); - return AliossFileSystemHandler.NAME.toLowerCase() + ":" + finalPath; + metadata.setContentType(contentType); + ossClient.putObject(bucket, filePath, inputStream, metadata); + return AliossFileSystemHandler.NAME.toLowerCase() + ":" + filePath; } @Override - public InputStream getData(String path) throws Exception { + public InputStream getData(String filePath) throws Exception { if (ossClient == null) { throw new FileStorageMediumHandlerNotFoundException("alioss"); } - return ossClient.getObject(Config.getConfigProperty("alioss.bucket", "neatlogic"), path.replaceAll(NAME.toLowerCase() + ":", "")).getObjectContent(); + filePath = filePath.replaceAll(NAME.toLowerCase() + ":", ""); + filePath = filePath.replaceAll(NAME.toUpperCase() + ":", ""); + return ossClient.getObject(Config.getConfigProperty("alioss.bucket", "neatlogic"), filePath).getObjectContent(); } @Override @@ -93,9 +127,10 @@ public class AliossFileSystemHandler implements InitializingBean, IFileStorageHa if (ossClient == null) { throw new FileStorageMediumHandlerNotFoundException("alioss"); } - if (StringUtils.isNotBlank(filePath) && filePath.startsWith(NAME.toLowerCase() + ":")) { - String path = filePath.replaceAll(NAME.toLowerCase() + ":", ""); - ossClient.deleteObject(Config.getConfigProperty("alioss.bucket", "neatlogic"), path); + if (StringUtils.isNotBlank(filePath)) { + filePath = filePath.replaceAll(NAME.toLowerCase() + ":", ""); + filePath = filePath.replaceAll(NAME.toUpperCase() + ":", ""); + ossClient.deleteObject(Config.getConfigProperty("alioss.bucket", "neatlogic"), filePath); } else { throw new FilePathIllegalException(filePath); } @@ -106,7 +141,9 @@ public class AliossFileSystemHandler implements InitializingBean, IFileStorageHa if (ossClient == null) { throw new FileStorageMediumHandlerNotFoundException("alioss"); } - return ossClient.getObjectMetadata(Config.getConfigProperty("alioss.bucket", "neatlogic"), filePath.replaceAll(NAME.toLowerCase() + ":", "")).getContentLength(); + filePath = filePath.replaceAll(NAME.toLowerCase() + ":", ""); + filePath = filePath.replaceAll(NAME.toUpperCase() + ":", ""); + return ossClient.getObjectMetadata(Config.getConfigProperty("alioss.bucket", "neatlogic"), filePath).getContentLength(); } @Override @@ -114,9 +151,8 @@ public class AliossFileSystemHandler implements InitializingBean, IFileStorageHa if (ossClient == null) { throw new FileStorageMediumHandlerNotFoundException("alioss"); } - ossClient.doesObjectExist(Config.getConfigProperty("alioss.bucket", "neatlogic"), filePath.replaceAll(NAME.toLowerCase() + ":", "")); - return true; + filePath = filePath.replaceAll(NAME.toLowerCase() + ":", ""); + filePath = filePath.replaceAll(NAME.toUpperCase() + ":", ""); + return ossClient.doesObjectExist(Config.getConfigProperty("alioss.bucket", "neatlogic"), filePath); } - - } -- Gitee From ed9f2032d70869684bcf7983cb1f15a92541dfbd Mon Sep 17 00:00:00 2001 From: "1437892690@qq.com" <1437892690@qq.com> Date: Fri, 19 Dec 2025 11:01:39 +0800 Subject: [PATCH 5/5] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]=20=E9=99=84=E4=BB=B6?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E5=B7=A5=E5=85=B7=E7=B1=BBFileUtil=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=8C=87=E5=AE=9A=E7=9B=AE=E6=A0=87=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1579604316225540]附件存储工具类FileUtil支持指定目标路径方式 http://192.168.0.96:8090/demo/rdm.html#/story-detail/939050947543040/939050947543042/1579604316225540 --- .../framework/common/util/FileUtil.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/main/java/neatlogic/framework/common/util/FileUtil.java b/src/main/java/neatlogic/framework/common/util/FileUtil.java index c13faff62..809ab78a8 100644 --- a/src/main/java/neatlogic/framework/common/util/FileUtil.java +++ b/src/main/java/neatlogic/framework/common/util/FileUtil.java @@ -53,10 +53,44 @@ public class FileUtil { handler = FileStorageMediumFactory.getHandler("FILE"); filePath = handler.saveData(tenantUuid, inputStream, file); } + } finally { + if (inputStream != null) { + inputStream.close(); + } } return filePath; } + /** + * 根据storageMediumHandler获取存储介质Handler,从而上传到对应的存储介质中 + * + * @param inputStream 文件流 + * @param contentType 文件类型 + * @param filePath 目标路径 + * @return 附件路径 + * @throws Exception 异常 + */ + public static String saveData(InputStream inputStream, String contentType, String filePath) throws Exception { + try { + IFileStorageHandler handler = FileStorageMediumFactory.getHandler(Config.FILE_HANDLER()); + if (handler == null) { + throw new FileStorageMediumHandlerNotFoundException(Config.FILE_HANDLER()); + } + return handler.saveData(inputStream, contentType, filePath); + } catch (Exception ex) { + logger.error(ex.getMessage(), ex); + if (!Objects.equals(Config.FILE_HANDLER(), "FILE")) { + IFileStorageHandler handler = FileStorageMediumFactory.getHandler("FILE"); + return handler.saveData(inputStream, contentType, filePath); + } + } finally { + if (inputStream != null) { + inputStream.close(); + } + } + return null; + } + /** * 获取附件 * @@ -112,4 +146,21 @@ public class FileUtil { return handler.getDataLength(filePath); } + /** + * 判断附件是否存在 + * @param filePath 附件路径 + * @return + * @throws Exception + */ + public static boolean exists(String filePath) throws Exception { + if (StringUtils.isBlank(filePath) || !filePath.contains(":")) { + throw new FilePathIllegalException(filePath); + } + String prefix = filePath.split(":")[0]; + IFileStorageHandler handler = FileStorageMediumFactory.getHandler(prefix); + if (handler == null) { + throw new FileStorageMediumHandlerNotFoundException(prefix); + } + return handler.isExit(filePath); + } } -- Gitee