05-SpringBoot 整合 FastDFS

SpringBoot 整合 FastDFS

添加POM依赖

<dependency>
    <groupId>com.github.tobato</groupId>
    <artifactId>fastdfs-client</artifactId>
    <version>1.26.7</version>
</dependency>

修改YAML配置

fdfs:
  connect-timeout: 30 # 连接的超时时间
  so-timeout: 30      # 读取的超时时间
  tracker-list: 192.168.247.142:22122 # tracker 服务所在的IP:端口

编写代码实现文件上传

package com.dance.redis.controller;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.upload.FastImageFile;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
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;

import java.io.IOException;

@RestController
@RequestMapping("/fdfs")
public class FastdfsController {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @PostMapping("/uploadFile")
    public String upload(@RequestParam("file") MultipartFile multipartFile){
        try {
            String originalFilename = multipartFile.getOriginalFilename();
            String extFileName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), extFileName, null);
            String group = storePath.getGroup();
            String path = storePath.getPath();
            return "http://192.168.247.143:8888/"+storePath.getFullPath();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

}

测试上传接口

使用ApiFox上传

测试成功返回数据, 去浏览器访问一下

可以访问, 上传成功

posted @ 2022-10-04 22:48  彼岸舞  阅读(65)  评论(0编辑  收藏  举报