上篇文章介紹了如何使用docker安裝fastDFS文件服務器,這一篇就介紹整合springBoot實現文件上傳到fastDFS文件服務器
1.pom.xml文件添加依賴
<!-- 連接fastdfs文件系統 --> <dependency> <groupId>net.oschina.zcx7878</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27.0.0</version> </dependency>
2.在resource包下創建配置文件fdfs_client.conf
tracker_server的值ip為你文件服務器的ip
connect_timeout=30
network_timeout=60
charset = UTF-8
http.tracker_http_port = 8888
http.anti_steal_token = no
http.secret_key =
tracker_server=ip:22122
3.創建FastDFSConfig.java加載fdfs_client.conf配置文件
import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.TrackerClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; /** * fastDFS文件上傳的配置 */ @Configuration public class FastDFSConfig { private final Logger log = LoggerFactory.getLogger(this.getClass()); @Value("classpath:fdfs_client.conf") private Resource ccs; @Bean public TrackerClient initClient(){ try{ ClientGlobal.init(ccs.getFilename()); return new TrackerClient(); }catch (Exception e){ log.info("FastDFS創建客戶端失敗"); return null; } } }
4.創建文件上傳的Cotroller,返回的訪問路徑中ip是你文件服務器的ip
import org.csource.common.NameValuePair; import org.csource.fastdfs.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController @RequestMapping("/file") public class FileController { private Logger log = LoggerFactory.getLogger(FileController.class); @Autowired private TrackerClient trackerClient; @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file) throws Exception{ if(file == null){ throw new RuntimeException("文件不能為空"); } //1.獲取文件的完整名稱 String filename = file.getOriginalFilename(); if(StringUtils.isEmpty(filename)){ throw new RuntimeException("文件不存在"); } //2.獲取文件的擴展名稱 String extName = filename.substring(filename.lastIndexOf(".") + 1); log.info("文件的全名:"+filename+" 文件的擴展名:"+extName); NameValuePair[] metaList = new NameValuePair[1]; metaList[0] = new NameValuePair("fileName", filename); //3.創建trackerServer TrackerServer trackerServer = trackerClient.getConnection(); // 4、創建一個 StorageServer 的引用,值為 null StorageServer storageServer = null; // 5、創建一個 StorageClient 對象,需要兩個參數 TrackerServer 對象、StorageServer 的引用 StorageClient storageClient = new StorageClient(trackerServer, storageServer); // 6、使用 StorageClient 對象上傳圖片。 String[] strings = storageClient.upload_file(file.getBytes(), extName, metaList); return "http://ip:8888/"+strings[0]+"/"+strings[1]; }
5.此時用postman調用你的文件上傳接口,根據返回的路徑在瀏覽器上訪問,即可成功訪問到你上傳的文件。