赞
踩
springboot中集成fastDFS 分布式文件存储系统
1、 pom.xml中引入
- <dependency>
- <groupId>com.github.tobato</groupId>
- <artifactId>fastdfs-client</artifactId>
- <version>1.27.2</version>
- </dependency>
-
2、 application.yml中配置
- spring:
- application:
- name: ssdemo
- servlet:
- multipart:
- max-file-size: 20MB
- max-request-size: 20MB
-
- fdfs:
- so-timeout: 1500 # socket连接超时时间
- connect-timeout: 6000 # 连接tracker超时时间
- thumb-image: #缩略图生成参数 ,可选
- width: 150
- height: 150
- tracker-list: #TrackerList参数,支持多个
- - 192.168.11.41:22122
- pool:
- #从池中借出的对象的最大数目(配置为-1表示不限制)
- max-total: -1
- #获取连接时的最大等待毫秒数,默认值5000
- max-wait-millis: 5000 #
- #每个key最大连接数
- max-total-per-key: 50
- #每个key对应的连接池最大空闲连接数
- max-idle-per-key: 10
- #每个key对应的连接池最小空闲连接数
- max_idle_per_key: 5
3、FastDFSConfig.java 配置类
- package cn.cbyzs.ssdemo.config;
-
-
- import com.github.tobato.fastdfs.FdfsClientConfig;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.EnableMBeanExport;
- import org.springframework.context.annotation.Import;
- import org.springframework.jmx.support.RegistrationPolicy;
-
- @Configuration
- @Import(FdfsClientConfig.class) // 导入FastDFS-Client组件
- @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) // 解决jmx重复注册bean的问题
- public class FastDFSConfig {
-
- }
4、Fdfs的上传controller
- package cn.cbyzs.ssdemo.other.controller;
-
- import cn.cbyzs.ssdemo.common.RetMsg;
- import cn.cbyzs.ssdemo.common.fastDFS.FastDFSClientUtil;
- import cn.cbyzs.ssdemo.common.jwt.JwtTokenIgnore;
- import org.springframework.beans.factory.annotation.Autowired;
- 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;
-
-
- @RestController
- @RequestMapping("/fdfs")
- public class FdfsController {
-
- @Autowired
- private FastDFSClientUtil fastDFSClientUtil;
-
-
- @JwtTokenIgnore
- @RequestMapping("/upload")
- public RetMsg upload(@RequestParam(value = "file",required = false)
- MultipartFile file){
- //调用 fdfsClient的上传接口
- String path =fastDFSClientUtil.uploadFile(file);
- return RetMsg.success(path);
- }
-
- }
测试上传 ,用postman调用controller层接口,上传一个图片成功
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。