SpringBoot整合fastdfs

女爷i 2024-04-03 10:25 160阅读 0赞

文章目录

  • 1、背景
  • 2、整合步骤
    • 2.1、引入依赖
    • 2.2、引入fastdfs配置
    • 2.3 编写文件上传和下载接口
    • 2.4 测试文件上传
    • 2.5 文件下载
  • 3、参考文档

1、背景

在前一节中,我们搭建了一个单机版的fastdfs服务,此处我们将fastdfs与springboot进行整合,实现文件的上传和下载。

2、整合步骤

2.1、引入依赖

  1. <dependency>
  2. <groupId>com.github.tobato</groupId>
  3. <artifactId>fastdfs-client</artifactId>
  4. <version>1.27.2</version>
  5. </dependency>

2.2、引入fastdfs配置

  1. fdfs:
  2. so-timeout: 2000 # 读取时间
  3. connect-timeout: 1000 # 连接超时时间
  4. thumb-image: # 生成缩略图
  5. height: 150 # 缩略图高度
  6. width: 150 # 缩略图宽度
  7. tracker-list: # tracker 服务器地址
  8. - 192.168.121.137:22122
  9. web-server-url: http://192.168.121.137:8888/ # storage 服务器上nginx的地址
  10. pool: # 可参考 ConnectionPoolConfig
  11. #从池中借出的对象的最大数目(配置为-1表示不限制)
  12. max-total: -1
  13. #获取连接时的最大等待毫秒数(默认配置为5秒)
  14. max-wait-millis: 5000
  15. #每个key最大连接数 key配置的是连接服务端的地址(IP+端口)连接情况,如果有连接不够用的情况可以调整以上参数
  16. max-total-per-key: 50
  17. #每个key对应的连接池最大空闲连接数
  18. max-idle-per-key: 10
  19. #每个key对应的连接池最小空闲连接数
  20. min-idle-per-key: 5
  21. #向调用者输出“连接”资源时,是否检测有效性
  22. test-on-borrow: true

2.3 编写文件上传和下载接口

  1. package com.huan.fastdfs.controller;
  2. import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
  3. import com.github.tobato.fastdfs.domain.fdfs.MetaData;
  4. import com.github.tobato.fastdfs.domain.fdfs.StorePath;
  5. import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
  6. import com.github.tobato.fastdfs.service.FastFileStorageClient;
  7. import lombok.AllArgsConstructor;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.commons.io.Charsets;
  10. import org.apache.commons.io.FileUtils;
  11. import org.apache.commons.io.FilenameUtils;
  12. import org.apache.commons.io.IOUtils;
  13. import org.springframework.stereotype.Controller;
  14. import org.springframework.web.bind.annotation.*;
  15. import org.springframework.web.multipart.MultipartFile;
  16. import javax.servlet.http.HttpServletResponse;
  17. import java.io.IOException;
  18. import java.net.URLEncoder;
  19. import java.util.ArrayList;
  20. import java.util.HashSet;
  21. import java.util.List;
  22. import java.util.Set;
  23. /**
  24. * fastdfs 文件上传和下载控制器
  25. * @author huan.fu
  26. * @date 2022/10/8 - 20:24
  27. */
  28. @RestController
  29. @AllArgsConstructor
  30. @RequestMapping("fdfs")
  31. @Slf4j
  32. public class FastdfsController {
  33. private final FastFileStorageClient fastFileStorageClient;
  34. private final FdfsWebServer fdfsWebServer;
  35. /**
  36. * 上传文件
  37. */
  38. @PostMapping("uploadFile")
  39. public List<String> uploadFile(MultipartFile file) throws IOException {
  40. String fileName = file.getOriginalFilename();
  41. // 获取文件扩展名
  42. String extension = FilenameUtils.getExtension(fileName);
  43. // 文件元数据信息
  44. Set<MetaData> metaData = new HashSet<>(4);
  45. metaData.add(new MetaData("fileName",fileName));
  46. // 上传文件
  47. StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), extension, metaData);
  48. log.info("文件上传路径:[{}]",storePath.getFullPath());
  49. String viewPath = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();
  50. log.info("可访问路径:[{}]",viewPath);
  51. extension = FilenameUtils.getExtension(viewPath);
  52. String xthumbPath = viewPath.replace("." + extension, "")+"_150x150."+extension;
  53. log.info("缩略图路径:[{}]",xthumbPath);
  54. List<String> result = new ArrayList<>(3);
  55. result.add(viewPath);
  56. result.add(xthumbPath);
  57. result.add(storePath.getFullPath());
  58. return result;
  59. }
  60. /**
  61. * 下载文件
  62. */
  63. @GetMapping("download")
  64. public void downloadFile(String filePath, HttpServletResponse response) throws IOException {
  65. log.info("需要下载的文件:[{}]",filePath);
  66. String group = filePath.substring(0, filePath.indexOf("/"));
  67. String path = filePath.substring(filePath.indexOf("/") + 1);
  68. Set<MetaData> metadata = fastFileStorageClient.getMetadata(group, path);
  69. String fileName = metadata.iterator().next().getValue();
  70. byte[] bytes = fastFileStorageClient.downloadFile(group, path, new DownloadByteArray());
  71. response.setContentType("application/octet-stream");
  72. response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, Charsets.UTF_8.displayName()));
  73. IOUtils.write(bytes,response.getOutputStream());
  74. }
  75. }

2.4 测试文件上传

fastdfs文件上传
从上图中可以,实现了文件的上传和缩略图的生成。

2.5 文件下载

fastdfs文件下载

3、参考文档

1、https://github.com/tobato/FastDFS_Client

发表评论

表情:
评论列表 (有 0 条评论,160人围观)

还没有评论,来说两句吧...

相关阅读

    相关 springboot整合fastdfs

    在项目开发中经常会碰到做文件上传的功能,一般来说,文件上传的步骤就那么几步,前台通过提交一个选中的文件,后端对文件做处理然后将文件上传至指定的地址,这个地址是一个真实的物理存储