飞道的博客

SpringBoot 整合 FastDFS

380人阅读  评论(0)

1. 创建SpringBoot 项目

2. 导入FastDFS Java客户端依赖

<!-- fastdfs -->
<dependency>
    <groupId>net.oschina.zcx7878</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27.0.0</version>
</dependency>

3. 配置 FastDFS 

3.1 在 application.properties 文件中配置如下:

#fastdfs 配置
fastdfs.connect_timeout_in_seconds=5
fastdfs.network_timeout_in_seconds=30
fastdfs.charset=UTF-8
fastdfs.http_anti_steal_token=false
fastdfs.http_secret_key=FastDFS1234567890
fastdfs.http_tracker_http_port=80
fastdfs.tracker_servers=192.168.31.225:22122
fastdfs.connection_pool.enabled=true
fastdfs.connection_pool.max_count_per_entry=500
fastdfs.connection_pool.max_idle_time=3600
fastdfs.connection_pool.max_wait_time_in_ms=1000
fastdfs.nginx.host=192.168.31.225

 3.2 创建FastDFS配置类

新建java文件 FastDFSConfig.java


  
  1. @ Configuration
  2. public class FastDFSConfig {
  3. @Bean
  4. public StorageClient storageClient() {
  5. StorageClient storageClient = null;
  6. try {
  7. ClientGlobal.initByProperties( "application.properties");
  8. TrackerClient trackerClient = new TrackerClient();
  9. TrackerServer trackerServer = trackerClient.getConnection();
  10. storageClient = new StorageClient(trackerServer, null);
  11. } catch (IOException | MyException e) {
  12. e.printStackTrace();
  13. }
  14. return storageClient;
  15. }
  16. }

4. 业务代码中使用

Service 层实现类


  
  1. @Service
  2. public class UploadServiceImpl implements UploadService {
  3. private static final Logger logger = LoggerFactory.getLogger(UploadServiceImpl. class);
  4. @Resource
  5. private StorageClient storageClient;
  6. @ Value( "${fastdfs.nginx.host}")
  7. private String serverAddr;
  8. @ Override
  9. public String upload(MultipartFile file) {
  10. String oldName = file.getOriginalFilename();
  11. if (oldName ! = null & & ! "".equals(oldName)) {
  12. String ext = oldName.substring(oldName.lastIndexOf( ".") + 1);
  13. try {
  14. String[] parts = storageClient.upload_ file( file.getBytes(), ext, null);
  15. String path = parts ! = null ? parts[ 0] + "/" + parts[ 1] : null;
  16. return "http://" + serverAddr + "/" + path;
  17. } catch (IOException | MyException e) {
  18. logger. error( "文件上传失败");
  19. e.printStackTrace();
  20. }
  21. }
  22. return null;
  23. }
  24. }

Controller 层调用


  
  1. @RestController
  2. @RequestMapping( "/fastdfs")
  3. public class UploadController {
  4. @Resource
  5. private UploadService uploadService;
  6. @PostMapping( "/upload")
  7. public String uploadFile(MultipartFile file) {
  8. System .out .println( ">>>>>>>>> 开始上传文件 >>>>>>>>>>>");
  9. return uploadService .upload(file);
  10. }
  11. }

5. postman 工具测试

 

返回结果就是上传到服务器的文件的访问地址,浏览器地址栏可以打开。

6. 封装工具类


  
  1. / **
  2. * FastDFS 文件服务工具类
  3. * @author: cs
  4. * @date: 2023-02-12 19:39:25
  5. * @since: 1.0
  6. */
  7. @Component
  8. public class FastDFSUtil {
  9. private static StorageClient 1 storageClient 1;
  10. public static final String FASTDFS_NGINX_HOST_SPREFIX = "http://";
  11. / / 服务器地址(服务器的Nginx已经配置好了,不需要加端口)
  12. @ Value( "${fastdfs.nginx.host}")
  13. private String serverAddr;
  14. / **
  15. * 初始化FastDFS 配置
  16. */
  17. @PostConstruct
  18. public void init() {
  19. try {
  20. ClientGlobal.initByProperties( "application.properties");
  21. / / 获取trackerClient服务
  22. TrackerClient trackerClient = new TrackerClient();
  23. TrackerServer trackerServer = trackerClient.getConnection();
  24. storageClient 1 = new StorageClient 1(trackerServer, null);
  25. } catch (IOException | MyException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. / **
  30. * 上传文件
  31. * @param file 文件对象
  32. * @return 文件id
  33. */
  34. public String upload(MultipartFile file) {
  35. String oldName = file.getOriginalFilename();
  36. if (oldName ! = null & & ! "".equals(oldName)) {
  37. String ext = oldName.substring(oldName.lastIndexOf( ".") + 1);
  38. try {
  39. return FASTDFS_NGINX_HOST_SPREFIX + serverAddr + "/" + storageClient 1.upload_ file 1( file.getBytes(), ext, null);
  40. } catch (IOException | MyException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. return null;
  45. }
  46. }

 使用时就不用再写 FastDFSConfig.java 配置类了,直接在类中使用 @Resource 注入即可。

@Resource
private FastDFSUtil fastDFSUtil;

7. 服务器中Nginx的配置

 在server 节点下配置以上内容。监听的端口是80,注意开防火墙。

/home/fastdfs/storage/data 是fastdfs 服务存储文件的位置

在安装Nginx 时不用额外安装类似 fastdfs-nginx-module 这个模块。直接在nginx.conf 中将fastdfs文件的存储路径配置一下即可。

 

 


转载:https://blog.csdn.net/qq_42026600/article/details/128998605
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场