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