From 117390c5aed5500f2a9d44656f04cc4a1f5c4c1f Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Thu, 9 Feb 2023 00:37:06 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E6=88=90=20FastDFS=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plusone-basic/plusone-basic-common/pom.xml | 7 +- .../plusone-basic-infrastructure/pom.xml | 9 +- .../zhouxy/plusone/oss/FastDFSException.java | 23 +++ .../xyz/zhouxy/plusone/oss/FastDFSUtil.java | 139 ++++++++++++++++++ .../src/main/resources/fdfs_client.conf | 8 + 5 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/oss/FastDFSException.java create mode 100644 plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/oss/FastDFSUtil.java create mode 100644 plusone-start/src/main/resources/fdfs_client.conf diff --git a/plusone-basic/plusone-basic-common/pom.xml b/plusone-basic/plusone-basic-common/pom.xml index 6fdebe7..9ada012 100644 --- a/plusone-basic/plusone-basic-common/pom.xml +++ b/plusone-basic/plusone-basic-common/pom.xml @@ -31,6 +31,11 @@ spring-webmvc + + commons-io + commons-io + + xyz.zhouxy.plusone plusone-commons @@ -44,7 +49,7 @@ xyz.zhouxy.plusone plusone-exception-handler - 0.0.6-SNAPSHOT + 0.0.7-SNAPSHOT diff --git a/plusone-basic/plusone-basic-infrastructure/pom.xml b/plusone-basic/plusone-basic-infrastructure/pom.xml index 548d37b..a195a10 100644 --- a/plusone-basic/plusone-basic-infrastructure/pom.xml +++ b/plusone-basic/plusone-basic-infrastructure/pom.xml @@ -1,5 +1,7 @@ - + 4.0.0 xyz.zhouxy @@ -84,6 +86,11 @@ com.tencentcloudapi tencentcloud-sdk-java-sms + + + org.csource + fastdfs-client-java + diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/oss/FastDFSException.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/oss/FastDFSException.java new file mode 100644 index 0000000..bfa367f --- /dev/null +++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/oss/FastDFSException.java @@ -0,0 +1,23 @@ +package xyz.zhouxy.plusone.oss; + +public class FastDFSException extends Exception { + + public FastDFSException() { + } + + public FastDFSException(String message) { + super(message); + } + + public FastDFSException(Throwable cause) { + super(cause); + } + + public FastDFSException(String message, Throwable cause) { + super(message, cause); + } + + public FastDFSException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/oss/FastDFSUtil.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/oss/FastDFSUtil.java new file mode 100644 index 0000000..aebc33f --- /dev/null +++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/oss/FastDFSUtil.java @@ -0,0 +1,139 @@ +package xyz.zhouxy.plusone.oss; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.csource.common.MyException; +import org.csource.common.NameValuePair; +import org.csource.fastdfs.ClientGlobal; +import org.csource.fastdfs.FileInfo; +import org.csource.fastdfs.StorageClient; +import org.csource.fastdfs.StorageServer; +import org.csource.fastdfs.TrackerClient; +import org.csource.fastdfs.TrackerServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.ClassPathResource; + +import lombok.Getter; + +public class FastDFSUtil { + + private static TrackerClient trackerClient; + private static TrackerServer trackerServer; + private static StorageServer storageServer; + + private static final Logger logger = LoggerFactory.getLogger(FastDFSUtil.class); + + private FastDFSUtil() { + throw new IllegalStateException("Utility class"); + } + + static { + try { + String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath(); + ClientGlobal.init(filePath); + trackerClient = new TrackerClient(); + trackerServer = trackerClient.getTrackerServer(); + storageServer = trackerClient.getStoreStorage(trackerServer); + } catch (Exception e) { + logger.error("FastDFS Client Init Fail!", e); + } + } + + /** + * 上传文件到 Fast DFS,失败将抛异常 + * + * @param file 文件信息 + * @return 2 elements string array if success:
+ * + * + * return null if fail + * @throws FastDFSException + */ + public static String[] upload(FastDFSFile file) throws FastDFSException { + logger.info("File Name: {}, File Length: {}", file.getName(), file.getContent().length); + + NameValuePair[] metaList = new NameValuePair[1]; + metaList[0] = new NameValuePair("author", file.getAuthor()); + + long startTime = System.currentTimeMillis(); + StorageClient storageClient = null; + String[] uploadResults = null; + try { + storageClient = new StorageClient(trackerServer, storageServer); + uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), metaList); + + if (uploadResults == null) { + throw new FastDFSException("upload file fail, error code: " + storageClient.getErrorCode()); + } + logger.info("Upload file successfully!!! group_name: {}, remoteFileName: {}, time used: {} ms", + uploadResults[0], uploadResults[1], System.currentTimeMillis() - startTime); + + } catch (IOException e) { + throw new FastDFSException("IO Exception when uploadind the file:" + file.getName(), e); + } catch (MyException e) { + throw new FastDFSException(e); + } + + return uploadResults; + } + + public static FileInfo getFile(String groupName, String remoteFileName) throws FastDFSException { + try { + StorageClient storageClient = new StorageClient(trackerServer, storageServer); + return storageClient.get_file_info(groupName, remoteFileName); + } catch (IOException e) { + throw new FastDFSException("IO Exception: Get File from Fast DFS failed", e); + } catch (Exception e) { + throw new FastDFSException("Non IO Exception: Get File from Fast DFS failed", e); + } + } + + public static InputStream downFile(String groupName, String remoteFileName) throws FastDFSException { + try { + StorageClient storageClient = new StorageClient(trackerServer, storageServer); + byte[] fileByte = storageClient.download_file(groupName, remoteFileName); + InputStream ins = new ByteArrayInputStream(fileByte); + return ins; + } catch (IOException e) { + throw new FastDFSException("IO Exception: Get File from Fast DFS failed", e); + } catch (Exception e) { + throw new FastDFSException("Non IO Exception: Get File from Fast DFS failed", e); + } + } + + public static void deleteFile(String groupName, String remoteFileName) throws FastDFSException { + StorageClient storageClient = new StorageClient(trackerServer, storageServer); + try { + int i = storageClient.delete_file(groupName, remoteFileName); + if (i == 0) { + logger.info("Delete file SUCCESSFULLY!!!"); + } else { + throw new FastDFSException("Delete file failed, error code is: " + i); + } + } catch (IOException | MyException e) { + throw new FastDFSException(e); + } + } + + @Getter + public static final class FastDFSFile { + private String name; + private byte[] content; + private String ext; + private String md5; + private String author; + + public FastDFSFile(String name, byte[] content, String ext) { + this.name = name; + this.content = content; + this.ext = ext; + } + } +} diff --git a/plusone-start/src/main/resources/fdfs_client.conf b/plusone-start/src/main/resources/fdfs_client.conf new file mode 100644 index 0000000..bd1a8ec --- /dev/null +++ b/plusone-start/src/main/resources/fdfs_client.conf @@ -0,0 +1,8 @@ +connect_timeout = 60 +network_timeout = 60 +charset = UTF-8 +http.tracker_http_port = 8080 +http.anti_steal_token = no +http.secret_key = 123456 + +tracker_server = 10.30.80.199:22122