SpringBoot 開發案例之整合FastDFS分散式檔案系統
阿新 • • 發佈:2021-07-22
1、pom依賴
<!--fastdfs--> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.6</version> </dependency>
2、application.properties
server.port=8082
#超時時長 fdfs.so-timeout=1500 #連線tracker伺服器超時時長 fdfs.connect-timeout=600 #縮圖 fdfs.thumb-image.height=150 fdfs.thumb-image.width=150 #tracker服務配置地址列表,替換成自己服務的IP地址,支援多個 fdfs.tracker-list=192.168.206.173:22122 #檔案上傳配置 spring.servlet.multipart.enabled=true spring.servlet.multipart.max-file-size=10MB spring.servlet.multipart.max-request-size=20MB
3、工具類
@Component publicclass FastdfsUtils { public static final String DEFAULT_CHARSET = "UTF-8"; @Autowired private FastFileStorageClient fastFileStorageClient; /** * 上傳 * * @param file * @return * @throws IOException */ public StorePath upload(MultipartFile file)throws IOException { // 設定檔案資訊 Set<MetaData> mataData = new HashSet<>(); mataData.add(new MetaData("author", "fastdfs")); mataData.add(new MetaData("description", file.getOriginalFilename())); // 上傳 StorePath storePath = fastFileStorageClient.uploadFile( file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null); return storePath; } /** * 刪除 * * @param path 例如: group1/M00/00/00/wKjOrWD45PKAY4xmAFLQaGXPnu0735.jpg */ public void delete(String path) { fastFileStorageClient.deleteFile(path); } /** * 刪除 * * @param group 例如: group1 * @param path 例如: M00/00/00/wKjOrWD45PKAY4xmAFLQaGXPnu0735.jpg */ public void delete(String group, String path) { fastFileStorageClient.deleteFile(group, path); } /** * 檔案下載 * * @param path 檔案路徑,例如:group1/M00/00/00/wKjOrWD40JiAQNKLABO5RCqSdcQ975.jpg * @param filename 下載的檔案命名 * @return */ public void download(String path, String filename, HttpServletResponse response) throws IOException { // 獲取檔案 StorePath storePath = StorePath.parseFromUrl(path); //如果名字是空的 下載檔名以儲存的為準 if (StringUtils.isBlank(filename)) { filename = FilenameUtils.getName(storePath.getPath()); } else { filename = filename + storePath.getPath().substring(storePath.getPath().lastIndexOf(".")); } byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray()); response.reset(); response.setContentType("applicatoin/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); ServletOutputStream out = response.getOutputStream(); out.write(bytes); out.close(); } }
4、controller層程式碼
@RestController public class FileController { private static final Logger LOGGER = LoggerFactory.getLogger(FileController.class); @Resource private FastdfsUtils fastdfsUtils; @PostMapping("uploadFile") private StorePath uploadFile(MultipartFile file) { StorePath storePath = null; try { storePath = fastdfsUtils.upload(file); } catch (Exception e) { e.printStackTrace(); LOGGER.info("服務異常"); } return storePath; } @PostMapping("deleteByPath") private String deleteByPath(String path) { try { fastdfsUtils.delete(path); } catch (Exception e) { e.printStackTrace(); LOGGER.info("刪除異常"); } return "success"; } @GetMapping("downloadFile") private void downloadFile(String path, String name, HttpServletResponse response) { try { fastdfsUtils.download(path, name, response); } catch (Exception e) { e.printStackTrace(); LOGGER.info("下載異常"); } } }