安装fastDFS及SpringBoot使用
下载fastDFS及nginx
链接:https://pan.baidu.com/s/1t4uAHgmDFi0W6fs8_1ZxeA
提取码:lakw
安装步骤
环境安装
yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y
安装libfastcommon
将libfastcommon、fastdfs和fastdfs-nginx解压文件放到 home/fdfs下
cd libfastcommon-master/
编译安装
./make.sh && ./make.sh install
若提示权限不够可使用chmod u+x *.sh设置权限
控制台出现以下信息表示编译并且安装成功
mkdir -p /usr/lib64
mkdir -p /usr/lib
mkdir -p /usr/include/fastcommon
安装fastdfs
cd fastdfs-master/
编译安装
./make.sh && ./make.sh install
若提示权限不够可使用chmod u+x *.sh设置权限
控制台出现以下信息表示编译并且安装成功
mkdir -p /usr/bin
mkdir -p /etc/fdfs
mkdir -p /usr/lib64
mkdir -p /usr/lib
配置tracker服务
将fastdfs配置文件全部复制到etc/fdfs/目录下
cp -r /home/fdfs/fastdfs-master/conf/* /etc/fdfs/
使用vim tracker.conf命令修改以下内容
base_path=/usr/local/fastdfs/tracker
创建存储日志和数据的根目录
mkdir -p /usr/local/fastdfs/tracker
使用以下命令启动tracker服务
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
使用ps -ef|grep tracker命令查看是否启动成功
root 9061 1 0 22:01 ? 00:00:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
root 9069 1768 0 22:01 pts/0 00:00:00 grep --color=auto tracker
使用以下命令停止tracker服务
/usr/bin/stop.sh /etc/fdfs/tracker.conf
配置storage服务
使用vim storage.conf命令修改以下内容
base_path=/usr/local/fastdfs/storage
store_path0=/usr/local/fastdfs/storage # storage的存储目录
tracker_server=192.168.32.128:22122 #ip根据实际tracker的ip地址配置
使用以下命令创建文件存储目录
mkdir /usr/local/fastdfs/storage
使用以下命令启动storage服务
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
使用ps -ef|grep storage命令查看是否启动成功
root 9124 1 0 22:16 ? 00:00:00 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
root 9128 1768 0 22:17 pts/0 00:00:00 grep --color=auto storage
使用以下命令停止storage服务
/usr/bin/stop.sh /etc/fdfs/storage.conf
配置client
使用vim client.conf命令修改以下内容
base_path=/usr/local/fastdfs/client
tracker_server=192.168.32.128:22122 #tracker的ip根据实际tracker的ip地址配置
使用以下命令创建目录
mkdir -p /usr/local/fastdfs/client
上次测试
fdfs_upload_file /etc/fdfs/client.conf /home/1.png
上次成功返回路径:group1/M00/00/00/wKgggGGoYHGAL79QAAAAAAAAAAA157.png
配置fastdfs-nginx
将fdfs和nginx整合配置文件复制到/etc/fdfs/目录
cp /home/fdfs/fastdfs-nginx-module-master/src/mod_fastdfs.conf /etc/fdfs
使用vim mod_fastdfs.conf命令修改以下内容
base_path=/usr/local/fastdfs/tmp
tracker_server=192.168.32.128:22122
url_have_group_name = true
store0_path=/usr/local/fastdfs/storage
使用以下命令创建目录
mkdir /usr/local/fastdfs/tmp
安装nginx
将nginx-1.20.0解压文件放到/home/下
使用以下命令创建nginx临时目录,否则启动会找不到目录
mkdir /var/temp/nginx -p
在/home/nginx-1.20.0目录下执行以下命令
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/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=/home/fdfs/fastdfs-nginx-module-master/src/
最后一行为fastdfs-nginx-module模块
编译及安装
make & make install
进入/usr/local/nginx/conf/目录,使用vim nginx.conf修改配置文件
#添加如下配置
server {
listen 80;
server_name localhost;
location ~/group[0-9]/ {
root /usr/local/fastdfs/storage/data
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
关闭防火墙
systemctl stop firewalld.service
启动nginx
cd /usr/local/nginx/sbin
./nginx 启动
./nginx -s stop 停止
启动fastDFS
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
停止fastDFS
/usr/bin/stop.sh /etc/fdfs/tracker.conf
/usr/bin/stop.sh /etc/fdfs/storage.conf
浏览器访问
SpringBoot整合fastDFS
添加依赖
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.2</version>
</dependency>
application.properties配置
fdfs.so-timeout=3000
fdfs.connect-timeout=3000
fdfs.tracker-list=192.168.32.128:22122
文件上传
/**
* 上传文件
*
* @param file参数
* @return 返回数据
* */
@Override
public String uploadFile(MultipartFile file) {
//获取文件后缀位置
int index = file.getOriginalFilename().lastIndexOf(".");
//文件后缀名
String fileSuffix = file.getOriginalFilename().substring(index + 1);
try {
//将文件上传到fastDFS
StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), fileSuffix, null);
String filePath = storePath.getFullPath(); //获取上传文件路径
logger.info("文件路径为:" + filePath);
return filePath;
}catch (Exception e){
e.printStackTrace();
logger.info("上传文件失败");
}
return null;
}
文件下载
@RequestMapping("/download")
public Map<String, Object> downloadFile(String fileName, HttpServletResponse response) {
fileService.downloadFile(fileName, response);
return null;
}
/**
* 下载文件
*
* @param context
* @param response
* */
@Override
public void downloadFile(String filePath, HttpServletResponse response) {
String group = filePath.substring(0, filePath.indexOf("/"));
String path = filePath.substring(filePath.indexOf("/") + 1);
String fileSuffix = filePath.substring(filePath.lastIndexOf(".") + 1);
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.lastIndexOf("."));
ServletOutputStream outputStream = null;
try{
//下载文件
byte[] bytes = fastFileStorageClient.downloadFile(group, path, new DownloadByteArray());
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName.concat(fileSuffix), "UTF-8"));
response.setCharacterEncoding("UTF-8");
outputStream = response.getOutputStream();
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
logger.info("下载文件失败");
} finally {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
文件删除
/**
* 删除文件
*
* @param context
* @return 返回数据
* */
@Override
public Map<String, Object> deleteFile(Map<String, Object> context) {
Map<String, Object> requestBody = ContextUtil.getRequestBody(context);
try {
String filePath = StringUtil.getAsString(requestBody.get("filePath"));
if(filePath == null || "".equals(filePath)){
throw new Exception("文件路径不能为空");
}
fastFileStorageClient.deleteFile(filePath);
}catch (Exception e){
e.printStackTrace();
logger.info("删除文件失败");
}
return null;
}