Springboot整合fastdfs實現(xiàn)分布式文件存儲
更新時間:2023年08月23日 11:46:52 作者:青春不散場
本文主要介紹了Springboot整合fastdfs實現(xiàn)分布式文件存儲,詳細(xì)闡述了Springboot應(yīng)用程序如何與FastDFS進(jìn)行集成及演示了如何使用Springboot和FastDFS實現(xiàn)分布式文件存儲,感興趣的可以了解一下
1、添加依賴
<dependency> <groupId>com.luhuiguo</groupId> <artifactId>fastdfs-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
2、添加配置項
# fdfs配置 fdfs: connect-timeout: 2000 # 連接服務(wù)器超時時間 so-timeout: 3000 tracker-list: ${ip}:${port} http: path: http://${ip}:${port}/ #http鏈接前綴
3、新建 fdfs_client.conf(可忽略)
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 = 192.168.53.85:22122
4、FastDFS客戶端工具
@Slf4j public class FastDFSClient { //分片客戶端 private static TrackerClient trackerClient; //分片服務(wù)端 private static TrackerServer trackerServer; //存儲桶客戶端 private static StorageClient storageClient; //存儲桶服務(wù)端 private static StorageServer storageServer; static { try { //引入配置 String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();; ClientGlobal.init(filePath); trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageServer = trackerClient.getStoreStorage(trackerServer); } catch (Exception e) { logger.error("FastDFS Client Init Fail!",e); } } //文件上傳 @SneakyThrows public static String[] upload(FastDFSFile file) { NameValuePair[] meta_list = new NameValuePair[1]; meta_list[0] = new NameValuePair("author", file.getAuthor()); long startTime = System.currentTimeMillis(); String[] uploadResults = null; storageClient = new StorageClient(trackerServer, storageServer); uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list); if(null == uploadResults) return null; String groupName = uploadResults[0]; String remoteFileName = uploadResults[1]; return uploadResults; } //獲取File @SneakyThrows public static FileInfo getFile(String groupName, String remoteFileName) { storageClient = new StorageClient(trackerServer, storageServer); return storageClient.get_file_info(groupName, remoteFileName); } //下載文件 @SneakyThrows public static InputStream downFile(String groupName, String remoteFileName) { storageClient = new StorageClient(trackerServer, storageServer); byte[] fileByte = storageClient.download_file(groupName, remoteFileName); InputStream ins = new ByteArrayInputStream(fileByte); return ins; } //刪除文件 @SneakyThrows public static void deleteFile(String groupName, String remoteFileName) { storageClient = new StorageClient(trackerServer, storageServer); int i = storageClient.delete_file(groupName, remoteFileName); logger.info("delete file successfully!!!" + i); } //獲取存儲器 @SneakyThrows public static StorageServer[] getStoreStorages(String groupName){ return trackerClient.getStoreStorages(trackerServer, groupName); } //獲取FetchStorages @SneakyThrows public static ServerInfo[] getFetchStorages(String groupName,String remoteFileName) { return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName); } // 獲取存儲器的URL @SneakyThrows public static String getTrackerUrl() { return "http://"+trackerServer.getInetSocketAddress().getHostString()+":" +ClientGlobal.getG_tracker_http_port()+"/"; } @Data public static class FastDFSFile { //文件名 private String name; //文件內(nèi)容 private byte[] content; //文件拓展名 private String ext; //文件的md5 private String md5; //文件做著 private String author; } }
到此這篇關(guān)于Springboot整合fastdfs實現(xiàn)分布式文件存儲的文章就介紹到這了,更多相關(guān)Springboot fastdfs分布式文件存儲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud Eureka 服務(wù)注冊實現(xiàn)過程
這篇文章主要介紹了SpringCloud Eureka 服務(wù)注冊實現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10Java靜態(tài)內(nèi)部類實現(xiàn)單例過程
這篇文章主要介紹了Java靜態(tài)內(nèi)部類實現(xiàn)單例過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10在Spring Boot中集成RabbitMQ詳細(xì)步驟(最新推薦)
本文將介紹如何在Spring Boot項目中集成RabbitMQ,實現(xiàn)生產(chǎn)者和消費(fèi)者的基本配置,本文分步驟給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-12-12解析SpringBoot @EnableAutoConfiguration的使用
這篇文章主要介紹了解析SpringBoot @EnableAutoConfiguration的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Java中的Opencv簡介與開發(fā)環(huán)境部署方法
OpenCV是一個開源的計算機(jī)視覺和圖像處理庫,提供了豐富的圖像處理算法和工具,它支持多種圖像處理和計算機(jī)視覺算法,可以用于物體識別與跟蹤、圖像分割與邊緣檢測、圖像特征提取與描述等應(yīng)用,本文介紹Java中的Opencv簡介與開發(fā)環(huán)境部署方法,感興趣的朋友一起看看吧2025-01-01