FastDFS分布式文件系统(详细入门级介绍)

574 篇文章 4 订阅

分布式文件系统 (Distributed File System) 是一个软件/软件服务器;
这个软件可以用来管理文件;
但这个软件所管理的文件通常不是在一个服务器节点上,而是在多个服务器节点上,这些服务器节点通过网络相连构成一个庞大的文件存储服务器集群,这些服务器都用于存储文件资源,通过分布式文件系统来管理这些服务器上的文件;
常见的分布式文件系统有:FastDFS、GFS、HDFS、Lustre 、Ceph 、GridFS 、mogileFS、TFS等;

分布式文件系统与传统文件系统对比:

在这里插入图片描述
简介:

FastDFS是一个开源的轻量级分布式文件系统,为互联网应用量身定做,简单、灵活、高效,采用C语言开发,由阿里巴巴开发并开源;

FastDFS对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载、文件删除)等,解决了大容量文件存储的问题,特别适合以文件为载体的在线服务,如相册网站、文档网站、图片网站、视频网站等等;

FastDFS充分考虑了冗余备份、线性扩容等机制,并注重高可用、高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务;

FastDFS系统架构从第一个版本发布后一直没有大的调整,高版本完全兼容低版本的数据,可以做到平滑升级,推荐更新升级到最新版本;

FastDFS代码托管在github上:

FastDFS整体架构

FastDFS文件系统由两大部分构成,一个是客户端,一个是服务端;

客户端

通常指我们的程序,比如我们的Java程序去连接FastDFS、操作FastDFS,那我们的Java程序就是一个客户端;
FastDFS提供专有API访问,目前提供了 C、Java 和 PHP 几种编程语言的API,用来访问FastDFS文件系统;

服务端
由两个部分构成:一个是跟踪器(tracker),一个是存储节点(storage);

跟踪器(tracker)主要做调度工作,在内存中记录集群中存储节点storage的状态信息,是前端Client和后端存储节点storage的枢纽;
因为相关信息全部在内存中,Tracker server的性能非常高,一个较大的集群(比如上百个group)中有3台就足够了;

存储节点(storage)用于存储文件,包括文件和文件属性(meta data)都保存到存储服务器磁盘上,完成文件管理的所有功能:文件存储、文件同步和提供文件访问等;
FastDFS线上使用者:
支付宝(http://www.alipay.com/)
京东商城(http://www.jd.com/)

FastDFS下载

FastDFS没有Windows版本,不能在Windows下使用;FastDFS需要安装部署在Linux环境下;
下载地址:

FastDFS安装:

安装前的准备
1> 检查Linux上是否安装了 gcc、libevent、libevent-devel,执行如下yum命令检查:

  yum list installed | grep gcc
  yum list installed | grep libevent
  yum list installed | grep libevent-devel

如果没有安装,则需进行安装,执行如下命令安装:

yum install gcc libevent libevent-devel -y  如果已经安装了就覆盖

2> 安装 libfastcommon 库
libfastcommon 库是 FastDFS 文件系统运行需要的公共 C 语言函数库;
安装libfastcommon 库的步骤如下:

  1. 下载 libfastcommon 源代码包;
  2. 下载地址:
  3. 解压下载下来的tar.gz压缩包:tar -zxvf libfastcommon-1.0.36.tar.gz
  4. 切换到解压后的libfastcommon目录:cd libfastcommon-1.0.36
  5. 执行make脚本进行编译:./make.sh
  6. 注意:第4步make编译的时候如果报错,需解决错误后再次进行make,通常发生错误是由于Linux缺少某些依赖库导致,根据错误提示解决错误;
    执行make install进行安装:./make.sh install

至此 libfastcommon 库安装完毕;
FastDFS才是我们真正的分布式文件系统,安装步骤如下:

1、下载FastDFS下载地址:
2、解压下载下来的tar.gz包:tar -zxvf fastdfs-5.11.tar.gz
3、切换到解压后的目录:cd fastdfs-5.11
4、执行编译: ./make.sh
5、然后再执行安装 ./make.sh install
至此FastDFS安装完成,安装后所有编译出来的文件存放在/usr/bin目录下,所有配置文件存放在/etc/fdfs目录下;
使用命令查看: ll /usr/bin/fdfs*
6、另外注意需要把解压后的fastdfs-5.11/conf目录下的两个文件拷贝到/etc/fdfs/
1、cp http.conf /etc/fdfs/
2、cp mime.types /etc/fdfs/
这两个文件后续需要用到,所以先拷贝过去;

FastDFS配置

FastDFS安装后配置文件位于/etc/fdfs/目录下,修改该目录下的配置文件;
把所有的.sample后缀都去掉,使用 mv 命令改文件名,去掉文件名后缀;
1、修改tracker.conf文件

base_path=/opt/fastdfs/tracker

2、修改storage.conf文件

base_path=/opt/fastdfs/storage
store_path0=/opt/fastdfs/storage/files
tracker_server=192.168.230.128:22122

然后启动FastDFS;

FastDFS启动

FastDFS服务启动需要启动两个脚本:
1、启动FastDFS的tracker服务,在任意目录下执行:fdfs_trackerd /etc/fdfs/tracker.conf
2、启动FastDFS的storage服务,在任意目录下执行:fdfs_storaged /etc/fdfs/storage.conf

首次启动storage后,它会在配置的路径下创建存储文件的目录;
查看启动进程:ps -ef | grep fdfs ,有启动的执行命令即为启动成功;
查看storage是否已经登记到了tracker下:fdfs_monitor /etc/fdfs/storage.conf

FastDFS关闭

关闭FastDFS,执行如下命令:
1、关闭tracker执行命令,在任意目录下执行:fdfs_trackerd /etc/fdfs/tracker.conf stop
2、关闭storage执行命令,在任意目录下执行:fdfs_storaged /etc/fdfs/storage.conf stop
或者kill关闭fastdfs,但不建议kill -9 强制关闭,因为可能会导致文件信息不同步问题;

FastDFS重启

重启FastDFS,执行如下命令:
重启tracker执行命令:fdfs_trackerd /etc/fdfs/tracker.conf restart
重启storage执行命令:fdfs_storaged /etc/fdfs/storage.conf restart

FastDFS测试

FastDFS安装完成之后,可以使用fdfs_test脚本测试文件上传,执行命令:

测试之前,需要修改client.conf配置文件,修改两个配置:

base_path=/opt/fastdfs/client
tracker_server=192.168.179.128:22122

测试文件上传,执行命令:

fdfs_test /etc/fdfs/client.conf upload /root/aa.txt

测试文件删除,执行命令:

fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKjmgVnaduyAIYxnAAAK-LjeM3Q088_big.txt

FastDFS生成的文件目录结构及名称示例:
在这里插入图片描述

安装FastDFS的Nginx扩展模块

概述
已经上传到FastDFS文件系统中的文件,我们如何在浏览器中访问呢?

FastDFS提供了一个Nginx扩展模块,利用该模块,我们可以通过Nginx访问已经上传到FastDFS上的文件;
下载
1、下载fastdfs的nginx扩展模块源代码:

wget https://codeload.github.com/happyfish100/fastdfs-nginx-module/zip/master

2、解压下载下来的fastdfs-nginx-module-master.zip 文件:

unzip fastdfs-nginx-module-master.zip

安装Nginx并且添加fastDFS模块
1、下载nginx:http://219.239.26.13/files/205400000A6F9D7F/nginx.org/download/nginx-1.12.1.tar.gz

2、解压下载下来的nginx文件,执行命令:tar -zxvf nginx-1.12.1.tar.gz

3、切换至解压后的nginx主目录,执行命令:cd nginx-1.12.1

4、切换到解压后的nginx-1.12.1目录下,执行configure命令:

./configure --prefix=/usr/local/nginx_fdfs --add-module=/root/soft/fastdfs-nginx-module-master/src

其中–prefix是指定nginx安装路径,–add-module指定fastDFS的nginx模块的源代码路径
5、执行命令进行编译:make
6、执行命令进行安装:make install
以上安装Nginx的FastDFS扩展模块注意事项
我们知道,Nginx的安装需要Linux安装相关的几个库,否则编译会出现错误,这几个库分别是:
1、gcc编译器是否安装?
检查是否安装:yum list installed | grep gcc
执行安装:yum install gcc -y
2、openssl库是否安装?
检查是否安装:yum list installed | grep openssl
执行安装:yum install openssl openssl-devel -y
3、pcre库是否安装?
检查是否安装:yum list installed | grep pcre
执行安装:yum install pcre pcre-devel -y
4、zlib库是否安装?
检查是否安装:yum list installed | grep zlib
执行安装:yum install zlib zlib-devel -y
配置
1、将/root/soft/fastdfs-nginx-module-master/src目录下的mod_fastdfs.conf文件拷贝到 /etc/fdfs/目录下,这样才能正常启动Nginx;
2、修改mod_fastdfs.conf配置文件:

base_path=/opt/fastdfs/nginx_mod
tracker_server=192.168.230.128:22122
store_path0=/opt/fastdfs/storage/files
url_have_group_name = true

3、配置Nginx的配置文件
#拦截请求路径中包含 /group[1-9]/M0[0-9] 的请求,使用 fastdfs的Nginx 模块进行转发:
location ~ /group[1-9]/M0[0-9] {
ngx_fastdfs_module;
}
如果出现404访问不到,检查一下 url_have_group_name = false

启动与测试

1、启动带有fastdfs模块的Nginx
2、重启fastDFS服务进程,执行如下命令:
fdfs_trackerd /etc/fdfs/tracker.conf restart
fdfs_storaged /etc/fdfs/storage.conf restart
3、上传一个文件进行测试验证:fdfs_test /etc/fdfs/client.conf upload /root/aa.txt
4、在浏览器访问上传的文件
当遇到400错误, 检查配置/etc/fdfs/mod_fastdfs.conf
url_have_group_name=true
该配置表示访问路径中是否需要带有group1,改为true表示路径中需要有group1

**

FastDFS提供的主要功能

upload:上传文件
download:下载文件
delete:删除文件

如下:

FastDFS服务器软件要在linux系统中启动

FastDFS文件系统的Java客户端

fastDFS文件系统Java客户端是指采用Java语言编写的一套程序,专门用来访问fastDFS文件系统;
1、 下载官方的源代码:

2、采用maven命令编译成jar安装到本地maven库:mvn clean install

3、在Java程序中使用它提供的API来访问fastDFS文件系统;

FastDFS开发示例

使用Java客户端,编程操作fastDFS分布式文件系统:

上传
下载
删除
pom文件 (fastdfs-client-java这个项目需要下载在本地编译成本地库文件)

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bjpowernode.fastdfs</groupId>
    <artifactId>18-fastdfs-java</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <dependency>
            <groupId>org.csource</groupId>
            <artifactId>fastdfs-client-java</artifactId>
            <version>1.27-SNAPSHOT</version>
        </dependency>

    </dependencies>
</project>

fdfs.conf文件

tracker_server=192.168.31.129:22122
tracker_server=192.168.31.130:22122

测试文件

package com.bjpowernode.fastdfs;

import org.csource.common.MyException;
import org.csource.fastdfs.*;

import java.io.IOException;

/**
 * ClassName:Test
 * Package:com.bjpowernode.fastdfs
 * Description:
 *
 * @date:2018/10/12 11:58
 * @author:bjpowernode.com
 */
public class Test {
    public static void main(String[] args) {
        upload();
//        download();
//        delete();
    }



    public static void upload(){
        StorageServer ss=null;
        TrackerServer ts= null;
        try {
            //加载配置文件,用于确定TrackerServer的地址
            ClientGlobal.init("fdfs.conf");
            //创建TrackerClient对象,用于连接TrackerServer和StorageServer
            TrackerClient tc=new TrackerClient();
            //获取TrackerServer ,并拦截到TrackerServer服务器
            ts=tc.getConnection();
            //根据获取TrackerServer获取StorageServer并拦截到StorageServer
            ss=tc.getStoreStorage(ts);
            //创建StorageClient对象,这个对象用于具体的操作FastDFS
            StorageClient storageClient=new StorageClient(ts,ss);

            //完成上传,上传后返回String数组,数组中的第一个元素为这个上传的文件所在组名,第二元素为这个上传的文件所在目录和文件名
            //我们需要将这个元素存入数据库中否则日后没有办法完成下载和删除
            String resultes[]= storageClient.upload_file("D:\\qrCode.jpg","jpg",null);

            for(String str:resultes){
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }finally {
            if(ss!=null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ts!=null){
                try {
                    ts.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static  void download(){
        StorageServer ss=null;
        TrackerServer ts= null;
        try {
            //加载配置文件,用于确定TrackerServer的地址
            ClientGlobal.init("fdfs.conf");
            //创建TrackerClient对象,用于连接TrackerServer和StorageServer
            TrackerClient tc=new TrackerClient();
            //获取TrackerServer ,并拦截到TrackerServer服务器
            ts=tc.getConnection();
            //根据获取TrackerServer获取StorageServer并拦截到StorageServer
            ss=tc.getStoreStorage(ts);
            //创建StorageClient对象,这个对象用于具体的操作FastDFS
            StorageClient storageClient=new StorageClient(ts,ss);
            //根据组名和远程文件名,下载文件并保存到本地中
            //返回值为整数 0表示文件下载完成 22表示目录错误 2 表示文件错误
           int result=storageClient.download_file("group1","M00/00/00/wKgfgFvAHm6AGwcwAAAqj2EiUAc048.jpg","E:\\abc.jpg");

            System.out.println(result);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }finally {
            if(ss!=null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ts!=null){
                try {
                    ts.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void delete(){
        StorageServer ss=null;
        TrackerServer ts= null;
        try {
            //加载配置文件,用于确定TrackerServer的地址
            ClientGlobal.init("fdfs.conf");
            //创建TrackerClient对象,用于连接TrackerServer和StorageServer
            TrackerClient tc=new TrackerClient();
            //获取TrackerServer ,并拦截到TrackerServer服务器
            ts=tc.getConnection();
            //根据获取TrackerServer获取StorageServer并拦截到StorageServer
            ss=tc.getStoreStorage(ts);
            //创建StorageClient对象,这个对象用于具体的操作FastDFS
            StorageClient storageClient=new StorageClient(ts,ss);
            //根据组名和远程文件名,删除文件
            //返回值为整数 0表示文件删除完成 22表示目录错误 2 表示文件错误
            int result=storageClient.delete_file("group12","M00/00/00/wKgfgFvAHm6AGwcwAAAqj2EiUAc048.jpg");
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }finally {
            if(ss!=null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ts!=null){
                try {
                    ts.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

FastDFS在web项目中的应用

完成一个文件上传至FastDFS的web示例;
在web项目中实现对文件的上传下载和删除操作;

 HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=" + creditor.getRemotefilename());

FastDFSUtil

package com.jidongcloud.fastdfs.util;

import org.csource.common.MyException;
import org.csource.fastdfs.*;

import java.io.IOException;

/**
 * ClassName:FastDFSUtil
 * Package:com.bjpowernode.fastdfs
 * Description:
 *
 * @date:2018/10/12 11:58
 * @author:bjpowernode.com
 */
public class FastDFSUtil {

    public static String[] upload(byte[] fileBuff, String extFileName) {
        StorageServer ss = null;
        TrackerServer ts = null;
        try {
            //加载配置文件,用于确定TrackerServer的地址
            ClientGlobal.init("fdfs.conf");
            //创建TrackerClient对象,用于连接TrackerServer和StorageServer
            TrackerClient tc = new TrackerClient();
            //获取TrackerServer ,并拦截到TrackerServer服务器
            ts = tc.getConnection();
            //根据获取TrackerServer获取StorageServer并拦截到StorageServer
            ss = tc.getStoreStorage(ts);
            //创建StorageClient对象,这个对象用于具体的操作FastDFS
            StorageClient storageClient = new StorageClient(ts, ss);

            //完成上传,上传后返回String数组,数组中的第一个元素为这个上传的文件所在组名,第二元素为这个上传的文件所在目录和文件名
            //我们需要将这个元素存入数据库中否则日后没有办法完成下载和删除
            String[] resultes = storageClient.upload_file(fileBuff, extFileName, null);
            return resultes;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        } finally {
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ts != null) {
                try {
                    ts.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


    public static byte[] download(String groupName, String remoteFileName) {
        StorageServer ss = null;
        TrackerServer ts = null;
        try {
            //加载配置文件,用于确定TrackerServer的地址
            ClientGlobal.init("fdfs.conf");
            //创建TrackerClient对象,用于连接TrackerServer和StorageServer
            TrackerClient tc = new TrackerClient();
            //获取TrackerServer ,并拦截到TrackerServer服务器
            ts = tc.getConnection();
            //根据获取TrackerServer获取StorageServer并拦截到StorageServer
            ss = tc.getStoreStorage(ts);
            //创建StorageClient对象,这个对象用于具体的操作FastDFS
            StorageClient storageClient = new StorageClient(ts, ss);
            //根据组名和远程文件名,下载文件并保存到本地中
            //返回值为整数 0表示文件下载完成 22表示目录错误 2 表示文件错误
            byte[] bytes = storageClient.download_file(groupName, remoteFileName);

            return bytes;

        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        } finally {
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ts != null) {
                try {
                    ts.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static void delete(String groupName, String remoteFileName) {
        StorageServer ss = null;
        TrackerServer ts = null;
        try {
            //加载配置文件,用于确定TrackerServer的地址
            ClientGlobal.init("fdfs.conf");
            //创建TrackerClient对象,用于连接TrackerServer和StorageServer
            TrackerClient tc = new TrackerClient();
            //获取TrackerServer ,并拦截到TrackerServer服务器
            ts = tc.getConnection();
            //根据获取TrackerServer获取StorageServer并拦截到StorageServer
            ss = tc.getStoreStorage(ts);
            //创建StorageClient对象,这个对象用于具体的操作FastDFS
            StorageClient storageClient = new StorageClient(ts, ss);
            //根据组名和远程文件名,删除文件
            //返回值为整数 0表示文件删除完成 22表示目录错误 2 表示文件错误
            int result = storageClient.delete_file(groupName, remoteFileName);
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        } finally {
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ts != null) {
                try {
                    ts.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

controller

package com.jidongcloud.fastdfs.controller;

import com.jidongcloud.fastdfs.model.Creditor;
import com.jidongcloud.fastdfs.service.CreditorService;
import com.jidongcloud.fastdfs.util.FastDFSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * ClassName:CreditorController
 * Package:com.bjpowernode.fastdfs.controller
 * Description:
 *  templates 目录下面的内容不能直接访问,需要转发才可以访问  forward SpringBoot已经默认添加了
 *  那么后缀的html 是templates 约定的你只要是html文件,我就默认给你加上后缀。
 *  所以我们只要写简单逻辑名称。
 * @date:2018/10/13 10:26
 * @author:bjpowernode.com
 */
@Controller
public class CreditorController {
    @Autowired
    private CreditorService creditorService;
    @RequestMapping("/")
    public String  index(Model model){

        List<Creditor>list=creditorService.selectAll();
        model.addAttribute("creditorList",list);
        //*  templates 目录下面的内容不能直接访问,需要转发才可以访问  forward SpringBoot已经默认添加了
        // *  那么后缀的html 是templates 约定的你只要是html文件,我就默认给你加上后缀。
        // *  所以我们只要写简单逻辑名称。
        return "creditors";
    }

    @RequestMapping("/goUpdate/{id}")
    public String goUpdate(@PathVariable Integer id, Model model){
        Creditor creditor= creditorService.selectCreditorById(id);
        model.addAttribute("creditor",creditor);
        return "upload";
    }
    @RequestMapping("/upload")
    //MultipartFile这个类是Spring定义的。获取前端传来的多媒体类型。
    //Integer id, MultipartFile uploadFile ,SpringMVC(有选择)接收参数方式
    //id 是表单中的name属性;uploadFile是表单中的MultipartFile属性
    public String upload(Integer id, MultipartFile uploadFile ){
        try {
            if(uploadFile.isEmpty()){
                System.out.println("返回上传页面给用户提示提示没有选择要上传的文件");
                return "";
        }
            //客户端以 io流的方式上传文件
            byte[] bytes = uploadFile.getBytes();//获取文件的字节数组
            String fileName=uploadFile.getOriginalFilename();//获取文件名 例如xx/dsfsd/dsfs.xxx
            String fileType= uploadFile.getContentType();//获取文件类型
            long fileSize=uploadFile.getSize();//获取文件大小
            String extFileName=fileName.substring(fileName.lastIndexOf(".")+1);//有可能会出错因为有些文件可以没有扩展名
            String[]results= FastDFSUtil.upload(bytes,extFileName);
            Creditor creditor=new Creditor();
            creditor.setId(id);
            creditor.setFileSize(fileSize);
            //fileName例如 : mysql-connection-java.jar
            creditor.setOldFileName(fileName);
            //application/text
            creditor.setFileType(fileType);
            //比如group1
            creditor.setGroupName(results[0]);//可能出现错,例如文件上传失败
            //比如 : M00/00/00/dfghj6788bnm文件
            creditor.setRemoteFileName(results[1]);
            creditorService.updateFile(creditor);

        }catch (Exception e){

        }
             //重定向到根路径请求
        return "redirect:/";
    }
    @RequestMapping("/download/{id}")
    public ResponseEntity<byte[]> download(@PathVariable Integer id){
        Creditor creditor= creditorService.selectCreditorById(id);
        byte[]bytes=  FastDFSUtil.download(creditor.getGroupName(),creditor.getRemoteFileName());
        HttpHeaders headers=new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=" + creditor.getOldFileName());
        headers.add("Content-Type",creditor.getFileType());//这是文件下载的类型,否则文件将以html格式完成下载
        headers.add("Content-Length",creditor.getFileSize()+"");//设置文件的大小,用于显示下载时的进度条
        ResponseEntity responseEntity=new ResponseEntity(bytes,headers, HttpStatus.OK);
        return responseEntity;
    }
    @RequestMapping("/delete/{id}")
    public String  delete(@PathVariable Integer id){

        creditorService.deleteFile(id);
        return "redirect:/";
    }
}

service

package com.jidongcloud.fastdfs.service.impl;


import com.jidongcloud.fastdfs.mapper.CreditorMapper;
import com.jidongcloud.fastdfs.model.Creditor;
import com.jidongcloud.fastdfs.service.CreditorService;
import com.jidongcloud.fastdfs.util.FastDFSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName:CreditorServiceImpl
 * Package:com.bjpowernode.fastdfs.service.impl
 * Description:
 *
 * @date:2018/10/13 10:29
 * @author:bjpowernode.com
 */
@Service("creditorService")
public class CreditorServiceImpl implements CreditorService {
    @Autowired
    private CreditorMapper creditorMapper;
    @Override
    public List<Creditor> selectAll() {

        return creditorMapper.selectAll();
    }

    @Override
    public Creditor selectCreditorById(Integer id) {
        return creditorMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateFile(Creditor creditor) {
        creditorMapper.updateByPrimaryKeySelective(creditor);
    }

    @Override
    public void deleteFile(Integer id) {
        Creditor creditor= creditorMapper.selectByPrimaryKey(id);
        FastDFSUtil.delete(creditor.getGroupName(),creditor.getRemoteFileName());


        creditorMapper.deleteFileById(id);

    }
}

mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.bjpowernode.fastdfs.mapper.CreditorMapper" >
  <resultMap id="BaseResultMap" type="com.bjpowernode.fastdfs.model.Creditor" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="real_name" property="realName" jdbcType="VARCHAR" />
    <result column="id_card" property="idCard" jdbcType="VARCHAR" />
    <result column="prhone" property="prhone" jdbcType="VARCHAR" />
    <result column="sex" property="sex" jdbcType="INTEGER" />
    <result column="contract_url" property="contractUrl" jdbcType="VARCHAR" />
    <result column="group_name" property="groupName" jdbcType="VARCHAR" />
    <result column="remote_file_name" property="remoteFileName" jdbcType="VARCHAR" />
    <result column="old_file_name" property="oldFileName" jdbcType="VARCHAR" />
    <result column="file_type" property="fileType" jdbcType="VARCHAR" />
    <result column="file_size" property="fileSize" jdbcType="BIGINT" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, real_name, id_card, prhone, sex, contract_url, group_name, remote_file_name, 
    old_file_name, file_type, file_size
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from creditor
    where id = #{id,jdbcType=INTEGER}
  </select>
  <select id="selectAll" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from creditor

  </select>

  <update id="deleteFileById">
    update creditor set remote_file_name='' where id=#{id}

  </update>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from creditor
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.bjpowernode.fastdfs.model.Creditor" >
    insert into creditor (id, real_name, id_card, 
      prhone, sex, contract_url, 
      group_name, remote_file_name, old_file_name, 
      file_type, file_size)
    values (#{id,jdbcType=INTEGER}, #{realName,jdbcType=VARCHAR}, #{idCard,jdbcType=VARCHAR}, 
      #{prhone,jdbcType=VARCHAR}, #{sex,jdbcType=INTEGER}, #{contractUrl,jdbcType=VARCHAR}, 
      #{groupName,jdbcType=VARCHAR}, #{remoteFileName,jdbcType=VARCHAR}, #{oldFileName,jdbcType=VARCHAR}, 
      #{fileType,jdbcType=VARCHAR}, #{fileSize,jdbcType=BIGINT})
  </insert>
  <insert id="insertSelective" parameterType="com.bjpowernode.fastdfs.model.Creditor" >
    insert into creditor
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="realName != null" >
        real_name,
      </if>
      <if test="idCard != null" >
        id_card,
      </if>
      <if test="prhone != null" >
        prhone,
      </if>
      <if test="sex != null" >
        sex,
      </if>
      <if test="contractUrl != null" >
        contract_url,
      </if>
      <if test="groupName != null" >
        group_name,
      </if>
      <if test="remoteFileName != null" >
        remote_file_name,
      </if>
      <if test="oldFileName != null" >
        old_file_name,
      </if>
      <if test="fileType != null" >
        file_type,
      </if>
      <if test="fileSize != null" >
        file_size,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="realName != null" >
        #{realName,jdbcType=VARCHAR},
      </if>
      <if test="idCard != null" >
        #{idCard,jdbcType=VARCHAR},
      </if>
      <if test="prhone != null" >
        #{prhone,jdbcType=VARCHAR},
      </if>
      <if test="sex != null" >
        #{sex,jdbcType=INTEGER},
      </if>
      <if test="contractUrl != null" >
        #{contractUrl,jdbcType=VARCHAR},
      </if>
      <if test="groupName != null" >
        #{groupName,jdbcType=VARCHAR},
      </if>
      <if test="remoteFileName != null" >
        #{remoteFileName,jdbcType=VARCHAR},
      </if>
      <if test="oldFileName != null" >
        #{oldFileName,jdbcType=VARCHAR},
      </if>
      <if test="fileType != null" >
        #{fileType,jdbcType=VARCHAR},
      </if>
      <if test="fileSize != null" >
        #{fileSize,jdbcType=BIGINT},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.bjpowernode.fastdfs.model.Creditor" >
    update creditor
    <set >
      <if test="realName != null" >
        real_name = #{realName,jdbcType=VARCHAR},
      </if>
      <if test="idCard != null" >
        id_card = #{idCard,jdbcType=VARCHAR},
      </if>
      <if test="prhone != null" >
        prhone = #{prhone,jdbcType=VARCHAR},
      </if>
      <if test="sex != null" >
        sex = #{sex,jdbcType=INTEGER},
      </if>
      <if test="contractUrl != null" >
        contract_url = #{contractUrl,jdbcType=VARCHAR},
      </if>
      <if test="groupName != null" >
        group_name = #{groupName,jdbcType=VARCHAR},
      </if>
      <if test="remoteFileName != null" >
        remote_file_name = #{remoteFileName,jdbcType=VARCHAR},
      </if>
      <if test="oldFileName != null" >
        old_file_name = #{oldFileName,jdbcType=VARCHAR},
      </if>
      <if test="fileType != null" >
        file_type = #{fileType,jdbcType=VARCHAR},
      </if>
      <if test="fileSize != null" >
        file_size = #{fileSize,jdbcType=BIGINT},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.bjpowernode.fastdfs.model.Creditor" >
    update creditor
    set real_name = #{realName,jdbcType=VARCHAR},
      id_card = #{idCard,jdbcType=VARCHAR},
      prhone = #{prhone,jdbcType=VARCHAR},
      sex = #{sex,jdbcType=INTEGER},
      contract_url = #{contractUrl,jdbcType=VARCHAR},
      group_name = #{groupName,jdbcType=VARCHAR},
      remote_file_name = #{remoteFileName,jdbcType=VARCHAR},
      old_file_name = #{oldFileName,jdbcType=VARCHAR},
      file_type = #{fileType,jdbcType=VARCHAR},
      file_size = #{fileSize,jdbcType=BIGINT}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

FastDFS分布式文件系统集群

在这里插入图片描述
以上完成达到水平:
熟悉分布式文件系统FastDFS的环境安装与部署,并具有项目应用经验;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值