diff --git a/src/main/java/neatlogic/framework/common/util/FileUtil.java b/src/main/java/neatlogic/framework/common/util/FileUtil.java index c13faff624f5f7a378bbbd6f87ff5684a6aa50e8..809ab78a82947d0e9347cfe0f4e701bf9e6c7677 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); + } } diff --git a/src/main/java/neatlogic/framework/file/core/IFileStorageHandler.java b/src/main/java/neatlogic/framework/file/core/IFileStorageHandler.java index 021a0cd3acd0c5e45c9754bad86b9aaef5d42b85..8827649bf974e830a2dc729c6d8cf294aea845f6 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; 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 8681d4492f4166e3a7b9e69f8400b64270d9a179..bd273977f4a9f024563fa52867301ddcf47b981f 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); } - - } 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 467f15b368f889106d2e94468bec4fd5248de3d2..b728ecb9ef8a82032ce2281ef0a440ffc204eda5 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; } } 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 432e0e089d46a12b1e4c3e195edead8a8f6d290c..d42d6abf5013d7183e125a6d8fc1faa8fc1d004e 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")) {