SpringBoot2.0整合Fastdfs 示例
阿新 • • 發佈:2018-12-09
1.簡介
自己在網上看了很多資料,寫的花裡胡巧的,到處是工具類,其實就幾行程式碼。
網上有兩種連線fastdfs的pom依賴,1:fastdfs-client,2:fastdfs-client-java,第二種看到別人的例項,太花了,不易讀,這裡使用的是第一種方式
還有的坑就是,好多人的用的都是springboot1.5版本,在pom引入fastdfs依賴(1.25.x)是有衝突的,最後在網上找到了原作者的git賬號,原作者的版本已經更新到1.26.2了,這裡用的是最新的
這裡把fastdfs的一些簡單操作程式碼分享下。
2.引入FastDfs依賴
3.application.yml配置檔案
# =================================================================== # 分散式檔案系統FDFS配置 # =================================================================== fdfs: so-timeout: 1501 connect-timeout: 2000 thumb-image: #縮圖生成引數 width: 150 height: 150 tracker-list: #TrackerList引數,支援多個 - www.gsdi.cc:9080
4.controller 介面編寫
package com.springbootfastdfs.controller; import com.github.tobato.fastdfs.domain.MataData; import com.github.tobato.fastdfs.domain.StorePath; import com.github.tobato.fastdfs.proto.storage.DownloadByteArray; import com.github.tobato.fastdfs.service.FastFileStorageClient; import com.sun.deploy.net.URLEncoder; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.io.FilenameUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashSet; import java.util.Set; @RestController @Api(description = "fastdfs介面API") public class TestApi { @Autowired private FastFileStorageClient fastFileStorageClient; /** * 檔案上傳 * * @param file * @return * @throws IOException */ @ApiOperation("檔案上傳") @PostMapping("/uppload") public StorePath test(@RequestParam MultipartFile file) throws IOException { // 設定檔案資訊 Set<MataData> mataData = new HashSet<>(); mataData.add(new MataData("author", "zonghui")); mataData.add(new MataData("description", "xxx檔案,嘿嘿嘿")); // 上傳 (檔案上傳可不填檔案資訊,填入null即可) StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), mataData); return storePath; } /** * 檔案刪除 * * @param path * @return */ @ApiOperation("檔案刪除") @DeleteMapping("/delete") public String delete(@RequestParam String path) { // 第一種刪除:引數:完整地址 fastFileStorageClient.deleteFile(path); // 第二種刪除:引數:組名加檔案路徑 // fastFileStorageClient.deleteFile(group,path); return "恭喜恭喜,刪除成功!"; } /** * 檔案下載 * * @param path * @return */ @ApiOperation("檔案下載") @GetMapping("/download") public void downLoad(@RequestParam String group, @RequestParam String path, @RequestParam String fileName, HttpServletResponse response) throws IOException { // 獲取檔案 byte[] bytes = fastFileStorageClient.downloadFile(group, path, new DownloadByteArray()); //設定相應型別application/octet-stream (注:applicatoin/octet-stream 為通用,一些其它的型別蘋果瀏覽器下載內容可能為空) response.reset(); response.setContentType("applicatoin/octet-stream"); //設定頭資訊 Content-Disposition為屬性名 附件形式開啟下載檔案 指定名稱 為 設定的fileName response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); // 寫入到流 ServletOutputStream out = response.getOutputStream(); out.write(bytes); out.close(); } }