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上传

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

可以访问, 上传成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值