工具类基于SpringBoot JDK8
1. pom文件–git下载源码,推送至maven私服或者自行在maven仓库搜索
1 <!-- fastdfs客户端 -->
2 <dependency>
3 <groupId>com.hy.fastdfs</groupId>
4 <artifactId>client</artifactId>
5 <!--去除日志依赖-->
6 <exclusions>
7 <exclusion>
8 <groupId>org.slf4j</groupId>
9 <artifactId>slf4j-log4j12</artifactId>
10 </exclusion>
11 </exclusions>
12 </dependency>
2. application.yml
1 fastdfs:
2 connect_timeout_in_seconds: 5
3 network_timeout_in_seconds: 10
4 charset: UTF-8
5 http_anti_steal_token: false
6 http_secret_key: FastDFS1234567890
7 http_tracker_http_port: 8888
8 http_server: http://127.0.0.1/
9 #以下是必选项
10 #多个用逗号隔开
11 tracker_servers: 127.0.0.1:22122
12 #url访问前缀
13 default_group_name: group1
14 url_prefix: group1
3. 自启动注解–用于启动类上
1 @Target(ElementType.TYPE)
2 @Retention(RetentionPolicy.RUNTIME)
3 @Documented
4 @Inherited
5 @Import({FastDFSClientConfig.class})
6 public @interface EnableFastDFSClient {
7 }
4. config配置类
1
6 @ConfigurationProperties(prefix = "fastdfs")
7 @Data
8 public class FastDFSConfig {
9 public static final FastDFSConfig config = new FastDFSConfig();
10
11 private String httpServer;
12
13 private static final String FASTDFS_URL_PREFIX = "fastdfs.url_prefix";
14
15 private int connectTimeoutInSeconds = ClientGlobal.DEFAULT_CONNECT_TIMEOUT;
16
17 private int networkTimeoutInSeconds = ClientGlobal.DEFAULT_NETWORK_TIMEOUT;
18
19 private String charset = ClientGlobal.DEFAULT_CHARSET;
20
21 private boolean httpAntiStealToken = ClientGlobal.DEFAULT_HTTP_ANTI_STEAL_TOKEN;
22
23 private String httpSecretKey = ClientGlobal.DEFAULT_HTTP_SECRET_KEY;
24 private int httpTrackerHttpPort = ClientGlobal.DEFAULT_HTTP_TRACKER_HTTP_PORT;
25
26 private String trackerServers;
27 private String urlPrefix;
28
29 private ConnectionPool connectionPool = new ConnectionPool();
30
31 @Data
32 public static class ConnectionPool {
33 private boolean enabled = ClientGlobal.DEFAULT_CONNECTION_POOL_ENABLED;
34 private int maxCountPerEntry = ClientGlobal.DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY;
35 private int maxIdleTime = ClientGlobal.DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME;
36 private int maxWaitTimeInMs = ClientGlobal.DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS;
37 }
38
39 }
5. 客户端配置
1
6 @Configuration
7 @EnableConfigurationProperties(FastDFSConfig.class)
8 public class FastDFSClientConfig {
9 @Resource
10 private FastDFSConfig fastdfsConfig;
11
12 @Bean
13 public StorageClient1 storageClient1() throws Exception {
14 return FastDFSUtil.init(fastdfsConfig);
15 }
16 }
6. 工具类
1
6 public class FastDFSUtil {
7 private static StorageClient1 storageClient1 = null;
8 private static String fastdfsServer = null;
9
10 public static StorageClient1 init(FastDFSConfig fastdfsConfig) throws Exception {
11 Properties properties = propertiesFromConfig(fastdfsConfig);
12 ClientGlobal.initByProperties(properties);
13
14 TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
15 TrackerServer trackerServer = trackerClient.getTrackerServer();
16 StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
17 storageClient1 = new StorageClient1(trackerServer, storageServer);
18 fastdfsServer = fastdfsConfig.getHttpServer();
19 return storageClient1;
20 }
21
22 private static Properties propertiesFromConfig(FastDFSConfig fastdfsConfig) {
23 Properties properties = new Properties();
24 properties.put(ClientGlobal.PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS, fastdfsConfig.getConnectTimeoutInSeconds());
25 properties.put(ClientGlobal.PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS, fastdfsConfig.getNetworkTimeoutInSeconds());
26 properties.put(ClientGlobal.PROP_KEY_CHARSET, fastdfsConfig.getCharset());
27 properties.put(ClientGlobal.PROP_KEY_HTTP_SECRET_KEY, fastdfsConfig.getHttpSecretKey());
28 properties.put(ClientGlobal.PROP_KEY_HTTP_ANTI_STEAL_TOKEN, fastdfsConfig.isHttpAntiStealToken());
29 properties.put(ClientGlobal.PROP_KEY_HTTP_TRACKER_HTTP_PORT, fastdfsConfig.getHttpTrackerHttpPort());
30 properties.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, fastdfsConfig.getTrackerServers());
31 properties.put(ClientGlobal.PROP_KEY_CONNECTION_POOL_ENABLED, fastdfsConfig.getConnectionPool().isEnabled());
32 properties.put(ClientGlobal.PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY, fastdfsConfig.getConnectionPool().getMaxCountPerEntry());
33
34 properties.put(ClientGlobal.PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME, fastdfsConfig.getConnectionPool().getMaxIdleTime());
35 properties.put(ClientGlobal.PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS, fastdfsConfig.getConnectionPool().getMaxWaitTimeInMs());
36 return properties;
37 }
38
39 public static String getFastdfsServer() {
40 return fastdfsServer;
41 }
42
43
46 public static StorageClient1 getStorageClient1() {
47 return storageClient1;
48 }
49
50
57 public static String uploadFile(InputStream inputStream, String fileExt) {
58 try {
59
60 byte[] fileByte = IOUtils.toByteArray(inputStream, 1024);
61 return storageClient1.upload_file1(fileByte, fileExt, null);
62 } catch (Exception ex) {
63 return null;
64 }
65 }
66
67
72 public static String uploadFile(byte[] fileByte, String fileExt) {
73 try {
74 return storageClient1.upload_file1(fileByte, fileExt, null);
75 } catch (Exception ex) {
76 return null;
77 }
78 }
79
80
81
87 public static int deleteFile(String fileId) {
88 try {
89 return storageClient1.delete_file1(fileId);
90 } catch (Exception ex) {
91 return -1;
92 }
93 }
94
95
103 public static String modifyFile(String oldFileId, byte[] fileByte, String fileExt) {
104 String fileId = null;
105 try {
106
107 fileId = uploadFile(fileByte, fileExt);
108 if (fileId == null) {
109 return null;
110 }
111
112 int delResult = deleteFile(oldFileId);
113 if (delResult != 0) {
114 return null;
115 }
116 } catch (Exception ex) {
117 return null;
118 }
119 return fileId;
120 }
121
122
127 public static byte[] downloadFile(String fileId) {
128 try {
129 return storageClient1.download_file1(fileId);
130 } catch (Exception ex) {
131 return null;
132 }
133 }
134 }