FastDFS

FastDFS入门

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

FastDFS安装

使用Docker安装FastDFS
拉取镜像

docker pull morunchang/fastdfs

运行tracker

docker run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh

运行storage

docker run -d --name storage --net=host -e TRACKER_IP=<your tracker server address>:22122 -e GROUP_NAME=<group name> morunchang/fastdfs sh storage.sh 

docker run -d --name storage --net=host -e TRACKER_IP=192.168.238.129:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh

在这里插入图片描述
修改nginx的配置

docker exec -it storage /bin/bash
vi /etc/nginx/conf/nginx.conf

添加以下内容

location ~ /M00 { 
	root /data/fast_data/data; 
	ngx_fastdfs_module; 
	#禁止浏览器缓存
	add_header Cache-Control no-store;
}

在这里插入图片描述

重启storage容器

exit
docker restart storage

查看tracker.conf和storage.conf配置文件

docker exec -it storage /bin/bash 
cd /etc/fdfs 
vim tracker.conf 
vim storage.conf

文件上传服务

 <!-- FastDFS依赖 -->
  <dependency>
      <groupId>com.github.tobato</groupId>
      <artifactId>fastdfs-client</artifactId>
      <version>1.26.7</version>
  </dependency>
server:
  port: 9002
spring:
  application:
    name: fastdfs-fileService
  servlet:
    multipart:
      enabled: true
      max-file-size: 10MB # 单个文件上传大小
      max-request-size: 20MB # 总文件上传大小

fdfs:
  # 链接超时
  connect-timeout: 60
  # 读取时间
  so-timeout: 60
  # 生成略缩图参数
  thumb-image:
    width: 150
    height: 150
  tracker-list: 192.168.238.129:22122

配置文件

@Configuration
@Import(FdfsClientConfig.class)
public class FDSConfig {
}

工具类

@Component
public class FDSUtil {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;


    public String upload(MultipartFile multipartFile) throws IOException {
        String extName = FilenameUtils.getExtension(multipartFile.getOriginalFilename());
        StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(), multipartFile.getSize(), extName, null);
        return storePath.getFullPath();
    }


    public void deleteFile(String fileUrl){
        StorePath storePath = StorePath.parseFromUrl(fileUrl);
        fastFileStorageClient.deleteFile(storePath.getGroup(),storePath.getPath());
    }
}

@RestController
public class FDSController {


    @Autowired
    private FDSUtil fdsUtil;

    @PostMapping(value = "/uploadFile",headers = "content-type=multipart/form-data")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
        String result = "";
        try {
            String upload = fdsUtil.upload(file);
            if (StringUtils.isEmpty(upload)) {
                result = "上传失败";
            } else {
                result = upload;
            }
        } catch (IOException e) {
            e.printStackTrace();
            result = "服务器异常";
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping(value = "/deleteByPath")
    public ResponseEntity<String> deleteByPath(String filePathName) {
        fdsUtil.deleteFile(filePathName);
        return ResponseEntity.ok("success");
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值