前言

参考网址

错误处理

安装

安装插件

yum install -y libevent
cd /usr/local
mkdir fastFDS
cd fastFDS
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz
mv V1.0.7.tar.gz libfastcommon-1.0.7.tar.gz
tar zxvf libfastcommon-1.0.7.tar.gz
cd libfastcommon-1.0.7/
./make.sh
./make.sh install ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so

tracker

cd /usr/local/fastFDS
wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz
mv V5.05.tar.gz FastDFS_v5.05.tar.gz
tar zxvf FastDFS_v5.05.tar.gz
cd fastdfs-5.05/
./make.sh
./make.sh install # 创建一个存放data和log的目录
mkdir -p /opt/fastdfs/tracker/data-and-log
# 配置
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
vim /etc/fdfs/tracker.conf
修改为:
base_path=/opt/fastdfs/tracker/data-and-log # 启动tracker服务:
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
# 重启tracker服务:
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
# 查看是否有 tracker 进程:
ps aux | grep tracker

storage

如果 storage 单独安装的话,那上面安装的所有步骤都要在走一遍,只是到了编辑配置文件的时候,编辑的是 storage.conf 而已

mkdir -p /opt/fastdfs/storage/data-and-log
mkdir -p /opt/fastdfs/storage/images-data cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
vim /etc/fdfs/storage.conf
修改:
base_path=/opt/fastdfs/storage/data-and-log
store_path0=/opt/fastdfs/storage/images-data
# 图片实际存放路径,如果有多个,这里可以有多行:
# store_path0=/opt/fastdfs/storage/images-data0
# store_path1=/opt/fastdfs/storage/images-data1
# store_path2=/opt/fastdfs/storage/images-data2 # 指定 tracker 服务器的 IP 和端口
tracker_server=192.168.63.128:22122 # 启动 storage 服务
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
# 重启 storage 服务:
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
# 查看是否有 storage 进程:
ps aux | grep storage

用自带的 client 进行测试

mkdir -p /opt/fastdfs/client/data-and-log

cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
vim /etc/fdfs/client.conf
修改:
base_path=/opt/fastdfs/client/data-and-log
# 指定 tracker 服务器的 IP 和端口
tracker_server=192.168.63.128:22122 # 下载并上传文件
cd /opt
wget http://img4.imgtn.bdimg.com/it/u=379667247,2270287117&fm=200&gp=0.jpg
mv 'u=379667247,2270287117&fm=200&gp=0.jpg' ceshi.jpg
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /opt/ceshi.jpg

想要查看结果必须安装nginx

cd /usr/local
wget http://nginx.org/download/nginx-1.11.8.tar.gz
wget http://jaist.dl.sourceforge.NET/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz mkdir -p /opt/setups/FastDFS
tar zxvf fastdfs-nginx-module_v1.16.tar.gz -C /opt/setups/FastDFS/fastdfs-nginx-module
vim /opt/setups/FastDFS/fastdfs-nginx-module/src/config
修改:
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"改为:
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
# 下面的一行也去掉local cp /usr/local/fastDFS/fastdfs-5.05/conf/http.conf /etc/fdfs
cp /usr/local/fastDFS/fastdfs-5.05/conf/mime.types /etc/fdfs # 安装 Nginx 依赖包
yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 预设几个文件夹,方便等下安装的时候有些文件可以进行存放
mkdir -p /opt/fastdfs/fastdfs-nginx-module/data-and-log
mkdir -p /usr/local/nginx /var/log/nginx /var/temp/nginx /var/lock/nginx
tar zxvf nginx-1.11.8.tar.gz
cd nginx-1.11.8
./configure --prefix=/usr/local/nginx --pid-path=/var/local/nginx/nginx.pid --lock-path=/var/lock/nginx/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --http-client-body-temp-path=/var/temp/nginx/client --http-proxy-temp-path=/var/temp/nginx/proxy --http-fastcgi-temp-path=/var/temp/nginx/fastcgi --http-uwsgi-temp-path=/var/temp/nginx/uwsgi --http-scgi-temp-path=/var/temp/nginx/scgi --add-module=/opt/setups/FastDFS/fastdfs-nginx-module/src
make
make install
cp /opt/setups/FastDFS/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs vim /etc/fdfs/mod_fastdfs.conf
base_path=/opt/fastdfs/fastdfs-nginx-module/data-and-log
tracker_server=192.168.63.128:22122
url_have_group_name = true
store_path0=/opt/fastdfs/storage/images-data
<!--图片实际存放路径,如果有多个,这里可以有多行:-->
<!--store_path0=/opt/fastdfs/storage/images-data0-->
<!--store_path1=/opt/fastdfs/storage/images-data1-->
<!--store_path2=/opt/fastdfs/storage/images-data2--> vim /usr/local/nginx/conf/nginx.conf
user root; listen 80;
# 访问本机
server_name 192.168.1.114;
# 拦截包含 /group1/M00 请求,使用 fastdfs 这个 Nginx 模块进行转发
location /group1/M00 {
ngx_fastdfs_module;
}
# 停掉防火墙:
service iptables stop
# 启动:
/usr/local/nginx/sbin/nginx
# 重启:
/usr/local/nginx/sbin/nginx -s reload
# 停止
/usr/local/nginx/sbin/nginx -s stop 访问nginx:
192.168.63.128
查看进程:
ps aux | grep nginx
错误信息:
vim /var/log/nginx/error.log

备份:

cp /usr/local/tracker/fastdfs-5.05/conf/http.conf /etc/fdfs
cp /usr/local/tracker/fastdfs-5.05/conf/mime.types /etc/fdfs

使用示例

引用

必须先安装到本地才可以引用

mvn install:install-file -DgroupId=org.csource.fastdfs -DartifactId=fastdfs  -Dversion=1.2 -Dpackaging=jar -Dfile=d:\setup\fastdfs_client_v1.20.jar
<dependency>
<groupId>org.csource.fastdfs</groupId>
<artifactId>fastdfs</artifactId>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>

配置

springMvc.xml

<!--配置可以直接读取的配置-->
<context:property-placeholder location="classpath:config/application.properties" />
<!-- 配置多媒体解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 设定文件上传的最大值5MB,5*1024*1024 -->
<property name="maxUploadSize" value="5242880"></property>
</bean>

fastdfs参数

fastDFS/fdfs_client.conf

# connect timeout in seconds
# default value is 30s
connect_timeout=30 # network timeout in seconds
# default value is 30s
network_timeout=60 # the base path to store log files
base_path=/opt/fastdfs/tracker/data-and-log # tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=192.168.63.128:22122 #standard log level as syslog, case insensitive, value list:
### emerg for emergency
### alert
### crit for critical
### error
### warn for warning
### notice
### info
### debug
log_level=info # if use connection pool
# default value is false
# since V4.05
use_connection_pool = false # connections whose the idle time exceeds this time will be closed
# unit: second
# default value is 3600
# since V4.05
connection_pool_max_idle_time = 3600 # if load FastDFS parameters from tracker server
# since V4.05
# default value is false
load_fdfs_parameters_from_tracker=false # if use storage ID instead of IP address
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# default value is false
# since V4.05
use_storage_id = false # specify storage ids filename, can use relative or absolute path
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# since V4.05
storage_ids_filename = storage_ids.conf #HTTP settings
http.tracker_server_port=80 #use "#include" directive to include HTTP other settiongs
##include http.conf

自定义工具类

FastDFSClient.java

package com.alvin.utils;

import org.apache.commons.io.FilenameUtils;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer; public class FastDFSClient { private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null; public FastDFSClient(String conf) throws Exception {
if (conf.contains("classpath:")) {
conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
} /**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileName 文件全路径
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
} /**
*这个比较好用
*/
public String uploadFile(byte[] file, String fileName, long fileSize) throws Exception {
NameValuePair[] metas = new NameValuePair[3];
metas[0] = new NameValuePair("fileName", fileName);
metas[1] = new NameValuePair("fileSize", String.valueOf(fileSize));
metas[2] = new NameValuePair("fileExt", FilenameUtils.getExtension(fileName));
String result = storageClient.upload_file1(file, FilenameUtils.getExtension(fileName), metas);
return result;
} public String uploadFile(String fileName) throws Exception {
return uploadFile(fileName, null, null);
} public String uploadFile(String fileName, String extName) throws Exception {
return uploadFile(fileName, extName, null);
} /**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileContent 文件的内容,字节数组
* @param extName 文件扩展名
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception { String result = storageClient.upload_file1(fileContent, extName, metas);
return result;
} public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
} public String uploadFile(byte[] fileContent, String extName) throws Exception {
return uploadFile(fileContent, extName, null);
}
}

使用

package com.alvin.controller;

import com.alvin.pojo.entity.Result;
import com.alvin.utils.FastDFSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; @RestController
@RequestMapping("/upload")
public class UploadController { @Value("${FILE_SERVER_URL}")
private String FILE_SERVER_URL; @RequestMapping("/uploadFile")
public Result upload(MultipartFile file) {
String fileName = file.getOriginalFilename();
try {
FastDFSClient fastDFSClient = new FastDFSClient("classpath:fastDFS/fdfs_client.conf");
String path = fastDFSClient.uploadFile(file.getBytes(), fileName, file.getSize());
String url = FILE_SERVER_URL + path;
return new Result(true, url); } catch (Exception e) {
e.printStackTrace();
return new Result(false, "上传失败");
}
}
}

fastdfs搭建和使用的更多相关文章

  1. FastDFS搭建分布式文件系统

    FastDFS搭建分布式文件系统 1. 什么是分布式文件系统 分布式文件系统(Distributed File System)是指文件系统管理的物理存储资源不一定直接连接在本地节点上,而是通过计算机网 ...

  2. [转帖]使用fastdfs搭建文件管理系统

    使用fastdfs搭建文件管理系统 https://www.jianshu.com/p/4e80069c84d3 今天同事说他们的系统用到了这个分布式文件管理系统. 一.FastDFS介绍 FastD ...

  3. FastDFS搭建单机图片服务器(二)

    防丢失转载:https://blog.csdn.net/MissEel/article/details/80856194 根据 分布式文件系统 - FastDFS 在 CentOS 下配置安装部署 和 ...

  4. FastDFS搭建单机图片服务器(一)

    防丢失转载:https://blog.csdn.net/MissEel/article/details/80856194 根据 分布式文件系统 - FastDFS 在 CentOS 下配置安装部署 和 ...

  5. FastDFS搭建及java整合代码【转】

    FastDFS软件介绍 1.什么是FastDFS FastDFS是用C语言编写的一款开源的分布式文件系统.FastDFS为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高 ...

  6. Centos7下用FastDFS搭建图片服务器

    1.所用到的工具: 1.FastDFS_v5.05.tar 2.fastdfs-nginx-module_v1.16.tar 3.libfastcommonV1.0.7.tar 4.nginx-1.1 ...

  7. FastDFS搭建

    a.上传安装时需要的文件 ①:FastDFS_v5.05.tar.gz ②:fastdfs-nginx-module_v1.16.tar.gz ③:libfastcommon-1.0.7.tar.gz ...

  8. FastDFS 搭建

    #FastDFS安装方式 安装必要插件:libevent  (此次搭建方案采用libevent 1.4.13)   wget http://fastdfs.googlecode.com/files/F ...

  9. Centos7 FastDFS 搭建

    安装libfastcommon 首先第一步是安装libfastcommon,我这里将libfastcommon上传到的/opt目录下,直接解压: yum -y install gcc-c++ yum ...

  10. FastDFS搭建文件管理系统

    参考:https://www.cnblogs.com/chiangchou/p/fastdfs.html 目录: 一:FastDFS介绍 1:简介: FastDFS 是一个开源的高性能分布式文件系统( ...

随机推荐

  1. Spring Security构建Rest服务-0100-前言

    一.我的前言 这是看慕课网老师讲的SpringSecurity的学习笔记,老师讲的很好,开篇就说到了我的心里,老师说道: 有一定经验的程序员如何提升自己? 1,每天都很忙,但是感觉水平没有提升 2,不 ...

  2. webpack4重新梳理一下2

    上一篇已经实现了webpack的基本打包操作,但是并没有使用配置文件,而是使用 CLI 来实现打包. 配置文件 // webpack.config.js module.exports = { //入口 ...

  3. elasticsearch(六) 之 elasticsearch优化

    目录 elasticsearch 优化 从硬件上 : 从软件上: 从用户使用层 elasticsearch 优化 从硬件上 : 使用SSD 硬盘,解决io导致的瓶颈. 增大内存 但不超过32G(单实例 ...

  4. jenkins 踩坑路 之 jenkins ssh 脚本

    背景: 由于公司业务调整,整个业务要从阿里云迁移到aws,自然 jenkins 也是要进行迁移的.jenkins 迁移过程中遇到的问题在此记录下,希望能给遇到类似问题的朋友些许帮助.也便于我后期遇到此 ...

  5. C/C++程序调试和内存检测

    程序出现错误很正常,一个优秀的程序员必须学会调试,发现错误并改正.减少程序错误最有效的方法是:在敲代码之前,多花点时间思考,如何构造程序,数据结构和算法,尽量把细节提前写下来,可以尝试着在纸上写出核心 ...

  6. 原生js实现拖动滑块验证

    拖动滑块验证是现在的网站随处可见的,各式各样的拖动法都有. 下面实现的是某宝的拖动滑块验证: <!DOCTYPE html> <html lang="en"> ...

  7. SSH 整合 (Maven)

    1.SSH 教程详见我的上一篇博客 SSH(Struts 2.3.31 + Spring 4.1.6 + Hibernate 5.0.12 + Ajax)框架整合实现简单的增删改查(包含分页,Ajax ...

  8. ZJOI2018 Round2 游记

    day0 高铁上颓了一部电影,然后闭上眼睛就到了 醒来之后发现被绑了艹,袖子被打了个结,搞了 \(20\) 分钟才解开,真想把绑我的人吊起来 \(xxx\) 公交车上碰到一位长者,被教育了一顿 长者: ...

  9. mysql replace语句

    语法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 REPLACE [LOW_PRIORITY | DELAYED]     [INTO] tbl_name     [ ...

  10. 自己实现一个双向绑定的Vue

    我们知道双向绑定是Vue的核心之一,接下来我们自己仿照Vue实现一个基本的功能. 项目代码在GitHub上: https://github.com/zhangKunUserGit/zk-vue