FastDFS--上传下载工具类
工具类基于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 /** 2 * @author huheng 3 * @desc fastdfs配置 4 * @date 2020-02-17 5 */ 6 @ConfigurationProperties(prefix = "fastdfs") 7 @Data 8 public class FastDFSConfig { 9 public static final FastDFSConfig config = new FastDFSConfig(); 10 11 private String httpServer; 12 // url前缀 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 // token 21 private boolean httpAntiStealToken = ClientGlobal.DEFAULT_HTTP_ANTI_STEAL_TOKEN; 22 // key 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 /** 2 * @author huheng 3 * @desc fastdfs客户端配置 4 * @date 2020-02-17 5 */ 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 /** 2 * @author huheng 3 * @desc fastdfs工具类 4 * @date 2020-02-17 5 */ 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 // 连接池 maxIdlePerKey 最大空闲连接数(影响并发性能) 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 /** 44 * 获取此{@link StorageClient1}就可以做想做的事[上传、下载、删除等] 45 */ 46 public static StorageClient1 getStorageClient1() { 47 return storageClient1; 48 } 49 50 /** 51 * 流的形式上传文件 52 * 53 * @param inputStream 54 * @param fileExt 55 * @return 56 */ 57 public static String uploadFile(InputStream inputStream, String fileExt) { 58 try { 59 // 将得到的文件流转二进制,便于后面直接将二进制存到fdfs服务器 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 /** 68 * @param fileByte 文件字节数组 69 * @param fileExt 文件扩展名 70 * @return 71 */ 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 /** 82 * 根据fileId来删除一个文件(我们现在用的就是这样的方式,上传文件时直接将fileId保存在了数据库中) 83 * 84 * @param fileId file_id源码中的解释file_id the file id(including group name and filename);例如 group1/M00/00/00/ooYBAFM6MpmAHM91AAAEgdpiRC0012.xml 85 * @return 0为成功,非0为失败,具体为错误代码 86 */ 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 /** 96 * 修改一个已经存在的文件 97 * 98 * @param oldFileId 原来旧文件的fileId, file_id源码中的解释file_id the file id(including group name and filename);例如 group1/M00/00/00/ooYBAFM6MpmAHM91AAAEgdpiRC0012.xml 99 * @param fileByte 新文件字节数组 100 * @param fileExt 新文件扩展名 101 * @return 102 */ 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 /** 123 * 文件下载 124 * 125 * @return 返回一个字节数组 126 */ 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 }

浙公网安备 33010602011771号