Spring Boot集成fastdfs快速入门Demo

1.什么是fastdfs

FastDFS 是一个开源的高性能分布式文件系统(DFS)。它的主要功能包括:文件存储,文件同步和文件访问,以及高容量和负载平衡。主要解决了海量数据存储问题,特别适合以中小文件(建议范围:4KB < file_size <500MB)为载体的在线服务。
FastDFS 系统有三个角色:跟踪服务器(Tracker Server)、存储服务器(Storage Server)和客户端(Client)。

  • Tracker Server:跟踪服务器,主要做调度工作,起到均衡的作用;负责管理所有的 storage server和 group,每个 storage 在启动后会连接 Tracker,告知自己所属 group 等信息,并保持周期性心跳。

  • Storage Server:存储服务器,主要提供容量和备份服务;以 group 为单位,每个 group 内可以有多台 storage server,数据互为备份。

  • Client:客户端,上传下载数据的服务器,也就是我们自己的项目所部署在的服务器。

9926a690335ae06b2fc01111a34c7021.png

目前fastdfs基本处于淘汰阶段,大家可以尝试用minio,具体介绍 >>> Spring Boot集成Minio快速入门demo

2.fastdfs环境搭建

搜索镜像

docker search fastdfs

拉取镜像(已经内置Nginx)

docker pull delron/fastdfs

构建Tracker

# 22122 => Tracker默认端口
docker run --name=tracker-server --privileged=true -p 22122:22122 -v /var/fdfs/tracker:/var/fdfs  --network=host -d delron/fastdfs tracker

构建Storage

# 23000 => Storage默认端口
# 8888 => 内置Nginx默认端口
# TRACKER_SERVER => 执行Tracker的ip和端口
# --net=host => 避免因为Docker网络问题导致外网客户端无法上传文件,因此使用host网络模式
docker run --name=storage-server --privileged=true -p 23000:23000 -p 8888:8888 -v /var/fdfs/storage:/var/fdfs -e TRACKER_SERVER=10.11.68.77:22122 -e GROUP_NAME=group1  --network=host -d delron/fastdfs storage

查看容器

docker ps

3.代码工程

 实验目的:实现文件上传

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>


    <artifactId>fastdfs</artifactId>


    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>

application.yaml

server:
  port: 8088
fdfs:
  soTimeout: 1500
  connectTimeout: 600
  thumbImage:             #thumbImage param
    width: 150
    height: 150
  trackerList:            #TrackerList参数,支持多个
    - 10.11.68.77:22122

controller

package com.et.fastdfs.controller;


import com.et.fastdfs.util.FastDFSClientWrapper;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
@RestController
public class HelloWorldController {
    @RequestMapping("/hello")
    public Map<String, Object> showHelloWorld(){
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "HelloWorld");
        return map;
    }
    @Resource
    private FastDFSClientWrapper dfsClient;
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> upload(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String fileUrl = dfsClient.uploadFile(file);
        map.put("file_url", fileUrl);
        return ResponseEntity.ok(map);


    }
}

util工具类

package com.et.fastdfs.util;


import com.et.fastdfs.constant.FastDFSConstants;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;


/**
 * Description: FastDFS文件上传下载包装类
 */
@Component
@Log4j2
public class FastDFSClientWrapper {


    @Autowired
    private FastFileStorageClient storageClient;


    /**
     * 上传文件
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public String uploadFile(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
        return getResAccessUrl(storePath);
    }


    /**
     * 将一段字符串生成一个文件上传
     * @param content 文件内容
     * @param fileExtension
     * @return
     */
    public String uploadFile(String content, String fileExtension) {
        byte[] buff = content.getBytes(Charset.forName("UTF-8"));
        ByteArrayInputStream stream = new ByteArrayInputStream(buff);
        StorePath storePath = storageClient.uploadFile(stream,buff.length, fileExtension,null);
        return getResAccessUrl(storePath);
    }


    // 封装图片完整URL地址
    private String getResAccessUrl(StorePath storePath) {
        String fileUrl = FastDFSConstants.HTTP_PRODOCOL + "://" + FastDFSConstants.RES_HOST + "/" + storePath.getFullPath();
        return fileUrl;
    }


    /**
     * 删除文件
     * @param fileUrl 文件访问地址
     * @return
     */
    public void deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            return;
        }
        try {
            StorePath storePath = StorePath.praseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (FdfsUnsupportStorePathException e) {
            log.warn(e.getMessage());
        }
    }


    // 除了FastDFSClientWrapper类中用到的api,客户端提供的api还有很多,可根据自身的业务需求,将其它接口也添加到工具类中即可。
    // 上传文件,并添加文件元数据
    //StorePath uploadFile(InputStream inputStream, long fileSize, String fileExtName, Set<MateData> metaDataSet);
    // 获取文件元数据
    //Set<MateData> getMetadata(String groupName, String path);
    // 上传图片并同时生成一个缩略图
    //StorePath uploadImageAndCrtThumbImage(InputStream inputStream, long fileSize, String fileExtName, Set<MateData> metaDataSet);
    // 。。。
}

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

  • https://github.com/Harries/springboot-demo

4.测试

  •  启动Spring Boot工程

  • postman上传文件测试

fe85f411ce0773ea44f9420f29dec54f.png

5.引用

  • https://www.cnblogs.com/cao-lei/p/13470695.html

  • https://github.com/ligohan/springboot-fastdfs-demo/tree/master?tab=readme-ov-file

  • http://www.liuhaihua.cn/archives/710431.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/558557.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

UDP文件传输工具之UDP怎么限流

UDP&#xff08;用户数据报协议&#xff09;以其低延迟和高速度的特点&#xff0c;在实时应用和大数据传输中扮演着重要角色。然而&#xff0c;UDP作为一种无连接的协议&#xff0c;并不保证数据包的顺序、完整性或可靠性。 因此&#xff0c;企业在寻求一种方式&#xff0c;有…

PCA(Principal Component Analysis,主成分分析)与矩阵X的协方差矩阵之间的联系

PCA&#xff08;Principal Component Analysis&#xff0c;主成分分析&#xff09;是一种常用的降维技术&#xff0c;用于将高维数据集投影到低维空间中。在PCA中&#xff0c;投影方程将原始特征向量 ( x 1 , x 2 , … , x p ) (x_1, x_2, \ldots, x_p) (x1​,x2​,…,xp​)映射…

服务器基本故障和排查方法

前言 服务器运维工作中遇到的问题形形色色&#xff0c;无论何种故障&#xff0c;都需要结合具体情况&#xff0c;预防为主的思想&#xff0c;熟悉各种工具和技术手段&#xff0c;养成良好的日志分析习惯&#xff0c;同时建立完善的应急预案和备份恢复策略&#xff0c;才能有效…

45、二叉树-二叉树的右视图

思路 层序遍历 从左向右遍历每一层取最后一个数&#xff0c;代码如下&#xff1a; public List<Integer> rightSideView(TreeNode root) {if (rootnull){return new ArrayList<>();}Queue<TreeNode> queue new LinkedList<>();List<Integer> …

Unity 中(提示框Tweet)

using UnityEngine; using UnityEngine.UI; using DG.Tweening; using System; public class Message : MonoBehaviour {public float dropDuration 0.5f; // 掉落持续时间public float persisterDuration 1f; // 持续显示时间public float dorpHeight;public static Message…

鸿蒙系列--ArkTS

一、ArkUI开发框架 ArkUI框架提供开发者两种开发方式&#xff1a;基于ArkTS的声明式开发范式和基于JS扩展的类Web开发范式。声明式开发范式更加简洁&#xff0c;类 Web 开发范式对 Web 及前端开发者更友好 二、ArkTS声明式开发范式 对比类 Web 开发范式代码更为精简&#xf…

用FRP配置toml文件搭建内网穿透

需求场景 1、一台外网可访问的有固定ip的云服务器&#xff0c;Ubuntu系统 2、一台外网无法访问的无固定ip的本地家用电脑&#xff0c;Ubuntu系统 需求&#xff1a;将云服务器搭建为一台内网穿透服务器&#xff0c;实现通过外网访问家用电脑&#xff08;网页&#xff09;的功能。…

奇怪的 NRST 管脚异常复位问题

1. 引言 本文探讨一个奇怪的 MCU NRST 管脚异常复位现象。 2. 复位问题及排查 这个问题是客户对开发的平台做 EMS 浪涌测试的时候发生的&#xff0c; 平台上使用了一个STM32G474 RCT6 MCU 。在某个等级的 EMS 测试中&#xff0c; 客户发现 MCU 有时候会异常复位而影响平台的…

c++使用googletest进行单元测试

googletest进行单元测试 使用Google test进行测试一、单元测试二、使用gmock测试 使用Google test进行测试 使用场景&#xff1a; 在平时写代码中&#xff0c;我们需要测试某个函数是否正确时可以使用Google test使用&#xff0c;当然&#xff0c;我们也可以自己写函数进行验证…

施耐德 PLC 及模块 ModbusTCP 通信配置方法

1. 通过【I/O扫描器】服务进行读写 相关文档&#xff1a;各模块说明书仅 NOE 网卡模块、部分 CPU 自带的网口支持 优点&#xff1a;不需要额外编程&#xff0c;系统自动周期型读写数据缺点&#xff1a;扫描周期不定&#xff0c;程序无法控制数据刷新的时序 2. 通过内部程序…

AJAX——图片上传

图片上传流程 1.获取图片文件对象 2.使用FormData携带图片文件 3.提交表单数据到服务器&#xff0c;使用图片url网址 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible"…

ESP32S3在VScode中使用USB口调试

ESP32S3在VScode中使用USB口调试 安装USB驱动修改工程的配置文件launch.jsonsettings.json 启动GDB Server 安装USB驱动 在powershell中输入下面指令&#xff1a; Invoke-WebRequest https://dl.espressif.com/dl/idf-env/idf-env.exe -OutFile .\idf-env.exe; .\idf-env.exe…

Ceph学习 -11.块存储RBD接口

文章目录 RBD接口1.基础知识1.1 基础知识1.2 简单实践1.3 小结 2.镜像管理2.1 基础知识2.2 简单实践2.3 小结 3.镜像实践3.1 基础知识3.2 简单实践3.3 小结 4.容量管理4.1 基础知识4.2 简单实践4.3 小结 5.快照管理5.1 基础知识5.2 简单实践5.3 小结 6.快照分层6.1 基础知识6.2…

MATLAB实现遗传算法优化公铁水联运

公铁水联运是运输行业的经典问题, 常用智能算法进行优化,比如遗传算法. 公铁水多式联运优化的数学模型如下&#xff1a; 1.模型简介 公铁水多式联运优化问题可以抽象为一个网络流问题&#xff0c;其中节点代表不同的运输方式转换点&#xff08;如公路、铁路、水运的交汇点&a…

探索MATLAB在计算机视觉与深度学习领域的实战应用

随着人工智能技术的快速发展&#xff0c;计算机视觉与深度学习已成为科技领域中最热门、最具挑战性的研究方向之一。 它们的应用范围从简单的图像处理扩展到了自动驾驶、医疗影像分析、智能监控行业等多个领域。 在这样的背景下&#xff0c;《MATLAB计算机视觉与深度学习实战…

【 基于Netty实现聊天室聊天业务学习】第4节.什么是BIO与NIO

IO在读写的时候是阻塞的&#xff0c;无法做其他操作&#xff0c;并发处理能力的非常低&#xff0c;线程之间访问资源通信时候也是非常耗时久&#xff0c;依赖我们的网速&#xff0c;带宽。 我们看一下他的白话原理 我们来看一下这张图那么这张图的话它里面有一个server还有三个…

OpenHarmony网络协议通信—libevent [GN编译] - 事件通知库

libevent主要是用C语言实现了事件通知的功能 下载安装 直接在OpenHarmony-SIG仓中搜索libevent并下载。 使用说明 以OpenHarmony 3.1 Beta的rk3568版本为例 库代码存放路径&#xff1a;./third_party/libevent 修改添加依赖的编译脚本 在/developtools/bytrace_standard/…

Office文档在线预览-文档内容在线提取

文档提取功能&#xff0c;支持文档内容提取。包括提取文档中的所有文字内容&#xff0c;提取文档中的图片&#xff0c;提取文档音视频&#xff0c;提取文档中的统计及图表等。同时支持文档中公式的提取&#xff0c;文档大纲目录提取等功能。 支持以下文档格式进行内容提取&…

OSPGF高级实验综合

1.实验拓扑 二.实验要求 1、R4为ISP&#xff0c;其上只配置IP地址&#xff1b;R4与其他所直连设备间均使用公有IP&#xff1b; 2、R3-R5、R6、R7为MGRE环境&#xff0c;R3为中心站点&#xff1b; 3、整个OSPF环境IP基于172.16.0.0/16划分&#xff1b;除了R12有两个环回&#x…

GPU深度学习环境搭建:Win10+CUDA 11.7+Pytorch1.13.1+Anaconda3+python3.10.9

1. 查看显卡驱动及对应cuda版本关系 1.1 显卡驱动和cuda版本信息查看方法 在命令行中输入【nvidia-smi】可以当前显卡驱动版本和cuda版本。 根据显示,显卡驱动版本为:Driver Version: 516.59,CUDA 的版本为:CUDA Version 11.7。 此处我们可以根据下面的表1 显卡驱动和c…
最新文章