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上传
测试成功返回数据, 去浏览器访问一下
可以访问, 上传成功
分类:
FastDFS与OSS
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
2020-10-04 CAS 原子操作
2020-10-04 简单理解Callable接口