当前位置:主页 > 服务器 > Nginx >

SpringBoot项目整合FastDFS+Nginx实现图片上传功能

时间:2022-11-08 09:50:17 | 栏目:Nginx | 点击:

FastDFS概述

Fastdfs原理

FastDFS包含Tracker Server和Storage Server; 客户端请求Tracker Server进行文件的上传与下载; Tracker Server调度Storage Server最终完成上传与下载。

上传文件流程

查询文件流程

安装Fastdfs

1、安装gcc

yum install -y gcc gcc-c++

2、下载libfastcommon到/usr/local下

cd /usr/local
wget https://github.com/happyfish200/libfastcommon/archive/V1.0.7.tar.gz

3、解压libfastcommon

tar -zxvf V1.0.7.tar.gz
cd libfastcommon-1.0.7

4、安装libfastcommon

./make.sh
./make.sh install

5、下载fastdfs

wget https://github.com/happyfish200/fastdfs/archive/V5.05.tar.gz

6、解压fastdfs并安装

tar -zxvf V5.05.tar.gz
cd fastdfs-5.05/
./make.sh
./make.sh install

7、将conf目录下的所有文件复制到/etc/fdfs/

cp /usr/local/fastdfs-5.05/conf/* /etc/fdfs/

8、配置tracker

cd /etc/fdfs
vi tracker.conf

主要配置

#端口号
port=22122   
#基础目录(Tracker运行时会向此目录存储storage的管理数据)
base_path=/usr/local/fastdfs   

如果base_path不存在,则需要创建目录

mkdir /usr/local/fastdfs

9、配置storage

cd /etc/fdfs
vi storage.conf

主要配置

#配置组名
group_name=group1
#端口
port=23000
#向tracker心跳间隔(秒)
heart_beat_interval=30
#基础目录,目录不存在,需要自行创建
base_path=/usr/local/fastdfs
#存放文件的位置,目录不存在,需要自行创建
store_path0=/usr/local/fastdfs/fdfs_storage 
#配置tracker服务器:IP
tracker_server=192.168.31.168:22122

10、启动服务 启动tracker

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

启动storage

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

查看服务

netstat -ntlp

整合Nginx模块

1、上传fastdfs-nginx-module_v1.16.tar.gz 到/usr/local 2、解压nginx模块

tar -zxvf fastdfs-nginx-module_v1.16.tar.gz

3、修改config文件,将文件中的 /usr/local/ 路径改为 /usr/

cd /usr/local/fastdfs-nginx-module/src
vi config

4、将fastdfs-nginx-module/src下的mod_fastdfs.conf拷贝至/etc/fdfs下

cp mod_fastdfs.conf /etc/fdfs/

5、修改/etc/fdfs/mod_fastdfs.conf

vi /etc/fdfs/mod_fastdfs.conf
内容:
base_path=/usr/local/fastdfs
tracker_server=192.168.31.168:22122 
#url中包含group名称
url_have_group_name=true       
#指定文件存储路径(上面配置的store路径) 
store_path0=/usr/local/fastdfs/fdfs_storage  

6、将libfdfsclient.so拷贝至/usr/lib下

cp /usr/lib64/libfdfsclient.so /usr/lib/

7、创建nginx/client目录

mkdir -p /var/temp/nginx/client

安装Nginx

1、 将nginx-1.8.0.tar.gz上传到/usr/local 2、解压:tar -zxvf nginx-1.8.0.tar.gz 3、安装依赖库

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

4、进入nginx解压的目录下:

cd /usr/local/nginx-1.8.0

5、安装

./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=/usr/local/fastdfs-nginx-module/src

编译、安装

make
make install

安装成功 6、拷贝配置文件http.conf和mime.types

cd /usr/local/fastdfs-5.0.5/conf
cp http.conf mime.types /etc/fdfs/

7、修改nginx配置文件

cd /usr/local/nginx/conf/
vi nginx.conf

8、关闭nginx,并启动nginx

pkill -9 nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

9、启动nginx

SpringBoot整合Fastdfs

1、创建SpringBoot项目 2、引入依赖

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
     <groupId>com.github.tobato</groupId>
     <artifactId>fastdfs-client</artifactId>
     <version>1.26.4</version>
 </dependency>

3、启动类上配置

@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)

4、配置文件

fdfs.so-timeout=3000
fdfs.connect-timeout=1000
fdfs.thumb-image.height=60
fdfs.thumb-image.width=60
fdfs.tracker-list=192.168.31.168:22122

5、控制器

@Controller
public class UploadController {
    public static final String DIR = "http://192.168.31.168/";
    @Autowired
    private FastFileStorageClient client;
    @RequestMapping("login")
    public String login(){
        return "login";
    }
    @ResponseBody
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public JsonResult upload(MultipartFile file) throws IOException {
        //获得后缀名
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        //上传
        StorePath storePath = client.uploadFile(file.getInputStream(), file.getSize(), extension, null);
        System.out.println("save:" + storePath.getFullPath());
        return new JsonResult(1,DIR + storePath.getFullPath());
    }
}

Java对象

public class JsonResult {
    private Integer code;
    private Object data;
    //get/set/constructor
 }

6、测试页面 页面使用了Vue+ElementUI

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>上传</title>
    <link rel="stylesheet" href="/elementui/index.css" rel="external nofollow"   >
    <style>
       ...
    </style>
</head>
<body>
    <div id="app">
        <el-card >
            <el-upload
                    class="avatar-uploader"
                    action="/upload"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-card>
    </div>
    <script src="/vue/vue.js"></script>
    <script src="/elementui/index.js"></script>
    <script>
        new Vue({
            el:"#app",
            data(){
                return{
                    imageUrl: ''
                }
            },
            methods:{
                handleAvatarSuccess(res, file) {
                    console.log(res);
                    this.imageUrl = res.data;
                }
            }
        })
    </script>
</body>
</html>

上传效果

您可能感兴趣的文章:

相关文章