FastDFS

本文详细介绍了FastDFS的安装、配置及Java操作全过程。首先,讲解了安装编译环境、FastDFS及其依赖库libfastcommon的安装步骤。接着,配置客户端、测试文件上传,并安装nginx及nginx-module,确保文件可以通过URL访问。最后,通过Java操作FastDFS,包括引入依赖、编写配置、上传页面及控制器代码,实现了文件的上传和读取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FastDFS

安装编译环境

1.由于FastDFS是C语音编写的,需要先安装C++库

yum -y install gcc-c++

2.安装fastDFS依赖库

yum -y install libevent

3.解压libfastcommon

tar -zxf libfastcommon

4.进入libfastcommon文件夹,编译libfastcommon

./make.sh

如果编译报错

./make.sh: line 99: perl: command not found

./make.sh: line 100: perl: command not found

说明没有安装perl,就要先安装perl

yum install perl

5.安装libfastcommon

./make.sh install

6.查看/usr/local/下有没libfastcommon.so

cd /usr/lib64
find -name libfastcommon.so

如果没有这个文件,则需要将/usr/lib/下的该文件拷贝到lib64中来

安装FastDFS

1.解压(下面所写的流程一步一步进行,切勿一次全部拷贝执行)

tar -zxf FastDFS_v5.08.tar.gz 
cd FastDFS
./make.sh
./make.sh install

编译报错的话,说明libfastcommon版本太低libfastcommon V1.0.39下载

2.拷贝conf下的配置文件到/etc/fdfs

cp * /etc/fdfs/

3.安装track服务,先修改/etc/fafs/track.conf下日志文件的存放地方

base_path=/usr/local/software/FastDFS/tracker

4.启动tracker服务,(用这个配置文件启动前面的服务)

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf

查看进程是否启动 ps -aux | grep tracker

5.重启使用命令

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

6.修改/etc/fafs/storage.conf

base_path=/usr/local/software/FastDFS/storage
store_path0=/usr/local/software/FastDFS/storage
tracker_server=192.168.9.128:22122

tracker_server务必不能使用localhost,使用实际ip地址

7.启动storage服务

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf

查看是否 启动ps -aux | grep storaged

8.查看是否启动成功(查看日志最后一行)

vi /usr/local/software/FastDFS/storage/logs/storaged.log

在这里插入图片描述

则成功

配置客户端

1.copy /usr/local/software/FastDFS/client下的文件libfdfsclient.so到/usr/lib64

cp libfdfsclient.so /usr/lib64

2.修改文件

vi /etc/fdfs/client.conf
base_path=/usr/local/software/FastDFS/client
测试文件上传

1.先放张图片timg.jpg到/usr,然后执行客户机上传指令

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /usr/timg.jpg 

在这里插入图片描述

上传成功,但是图片的地址http://192.168.9.128/group1/M00/00/00/wKgJgFvo5S6ARHYVAAI054nIW9I142_big.jpg无法访问,这时候就需要nginx了

安装nginx-module

1.安装nginx的环境

yum -y install pcre pcre-devel 
yum install -y zlib-devel 
yum install -y openssl openssl-devel

2.安装nginx

tar -zxf fastdfs-nginx-module
cd fastdfs-nginx-module/src
vi config 

3.文件指令先按esc :0,$s//local//g 表示替换/local为空

 :0,$s/\/local//g

4.配置mod_fastdfs.conf

vi /etc/fdfs/mod_fastdfs.conf 
tracker_server=192.168.9.128:22122
url_have_group_name = true
store_path0=/usr/local/software/FastDFS/storage

url_have_group_name设置为true,则表示会默认使用组名

安装nginx

目的是:将具有/group/M00/请求的地址交给nginx_module处理

tar -zxf nginx -1.0.11
cd nginx -1.0.11
./configure --add-module=/usr/local/software/fastdfs-nginx-module/src/
make
make install

1.进入nginx核心配置文件,配置nginx.conf,将具有/group/M00/请求的地址交给nginx_module处理

cd /usr/local/nginx/conf/
vi nginx.conf
location /group1/M00/ {
	ngx_fastdfs_module;
}

2.保存之后进入nginx核心配置路径/usr/local/nginx下的sbin,启动nginx

cd /usr/local/nginx/sbin
./nginx

3.查看nginx运行状态

ps -aux | grep nginx

在这里插入图片描述

看到master和worker则nginx启动成功

4.开放CenterOS7 的80端口,并重启防火墙

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

查看之前无法访问的图片地址

http://192.168.9.128/group1/M00/00/00/wKgJgFvo5S6ARHYVAAI054nIW9I142_big.jpg

在这里插入图片描述

成功!

Java操作FastDFS
1.引入pom
<!--FastDFS-->
        <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.1-RELEASE</version>
        </dependency>
2.编写application.yml
fdfs:
  connect-timeout: 1000
  #上传缩略图的高宽
  thumb-image:
    height: 200
    width: 200
  pool:
    jmx-enabled: false
    #trake服务地址,list务必用-key: value的方式使用
  tracker-list:
  - 192.168.9.128:22122
3.编写上传页面

<form th:action="${#request.getContextPath()+'/submit'}" method="post" enctype="multipart/form-data">
    请选择文件:<input type="file" name="file" multiple="true"/>
    <input type="submit" value="提交">
</form>

4.编写controller类
    @RequestMapping("/submit")
    @ResponseBody
    public void submit(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println("----图片开始上传----");
        //正常文件上传,如果上传大小有限制,务必在app.yml上进行
        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), "mp3", null);

        //图片上传到FastDFS并保存缩略图
//        StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), "jpg", null);

        System.err.println("图片上传的路径是:"+storePath);
        System.err.println("fullpath:"+storePath.getFullPath());
        System.err.println("group:"+storePath.getGroup());
        System.err.println("path:"+storePath.getPath());
    }


上传文件(MP4,MP3)的话必须使用uploadFile方法,uploadImageAndCrtThumbImage方法用于上传图片和缩略图;如果页面上传过程报文件大小超标,那么在配置文件设置spring.servlet.multipartmultipart.max-file-size:XXMB

上传的文件如何读取

通过FastDFS的服务器ip+fullpath即可读取

例如:http://192.168.9.128/group1/M00/00/00/wKgJgFvo5S6ARHYVAAI054nIW9I142_big.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值