FastDFS简介,运用

在这里插入图片描述
在这里插入图片描述

springboot集成fastdfs:
在这里插入图片描述
额外加的依赖:

<dependency>
	<groupId>com.github.tobato</groupId>
	<artifactId>fastdfs-client</artifactId>
	<version>1.26.7</version>
</dependency>

在这里插入图片描述
upload.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上传</title>
</head>
<body>

<form method="post" action="/upload/uploadFile" enctype="multipart/form-data">
    <input type="file" name="mf">

    <input type="submit" value="提交">

</form>

<form method="post" action="/upload/deleteFile">
    <input type="text" name="path">

    <input type="submit" value="删除">

</form>

</body>
</html>

在这里插入图片描述
在这里插入图片描述

@Service
@EnableConfigurationProperties(UploadProperties.class)
public class UploadService {

    private Log log= LogFactory.getLog(UploadService.class);

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private UploadProperties prop;

    public String uploadImage(MultipartFile file) {
        // 1、校验文件类型
        String contentType = file.getContentType();
        if (!prop.getAllowTypes().contains(contentType)) {
            throw new RuntimeException("文件类型不支持");
        }
        // 2、校验文件内容
        try {
            BufferedImage image = ImageIO.read(file.getInputStream());
            if (image == null || image.getWidth() == 0 || image.getHeight() == 0) {
                throw new RuntimeException("上传文件有问题");
            }
        } catch (IOException e) {
            log.error("校验文件内容失败....{}", e);
            throw new RuntimeException("校验文件内容失败"+e.getMessage());
        }

        try {
            // 3、上传到FastDFS
            // 3.1、获取扩展名
            String extension = StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
            // 3.2、上传
            StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), extension, null);
            // 返回路径
            return storePath.getFullPath();
        } catch (IOException e) {
            log.error("【文件上传】上传文件失败!....{}", e);
            throw  new RuntimeException("【文件上传】上传文件失败!"+e.getMessage());
        }
    }

    /**
     * 文件删除
     * @param path
     */
    public void deleteFile(String path) {
            storageClient.deleteFile(path);
        log.info("【文件删除】文件删除成功!....{}");
    }
}

@RestController
@RequestMapping("upload")
public class UploadController {
    @Autowired
    private UploadProperties prop;


    @Autowired
    private UploadService uploadService;


    @RequestMapping("uploadFile")
    public Map<String,Object> uploadFile(MultipartFile mf)
    {
        String path = uploadService.uploadImage(mf);
        Map<String,Object> map=new HashMap<>();
        map.put("code",1);
        map.put("msg","");
        map.put("path",prop.getBaseUrl()+path);
        return map;
    }

    @RequestMapping("deleteFile")
    public Map<String,Object> uploadFile(String path)
    {
        uploadService.deleteFile(path);
        Map<String,Object> map=new HashMap<>();
        map.put("code",1);
        map.put("msg","删除成功");
        return map;
    }

}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
这样逃宝图片就打开了。

**

fastdfs+nginx

**
9.1 在tracker上安装 nginx
在每个tracker上安装nginx,的主要目的是做负载均衡及实现高可用。如果只有一台tracker可以不配置nginx。
一个tracker对应多个storage,通过nginx对storage负载均衡;
9.2 在storage 上安装nginx
(1)上传fastdfs-nginx-module-1.20.tar.gz 到Centos服务器上;

(2)解压fastdfs-nginx-module-1.20.tar.gz 并移动到 /usr/local目录下;

tar -zxvf fastdfs-nginx-module-1.20.tar.gz 解压
(3)切换目录到: fastdfs-nginx-module-1.20/src 目录下

cd fastdfs-nginx-module-1.20/src
(4)修改config文件,将文件中的所有 /usr/local/ 路径改为 /usr/
修改之后
在这里插入图片描述

(5)将fastdfs-nginx-module/src下的mod_fastdfs.conf拷贝至/etc/fdfs/下

cp mod_fastdfs.conf /etc/fdfs/
(6)并修改 /etc/fdfs/mod_fastdfs.conf 的内容;

vi /etc/fdfs/mod_fastdfs.conf
tracker_server=117.48.203.125:22122

#tracker_server=192.168.172.20:22122 #(多个tracker配置多行)
url_have_group_name=true #url中包含group名称

store_path0=/home/fdfs_storage #指定文件存储路径(上面配置的store路径)

9.3 进入之前解压的fastdfs目录下,把http.conf、mime.conf移动至/etc/fdfs

cp http.conf mime.types /etc/fdfs/


10, Nginx的安装
10.1,上传 nginx-1.15.2.tar.gz 到Centos服务器上;

10.2,解压 nginx-1.15.2.tar.gz

cd /fileservice/fast/
tar -zxvf nginx-1.15.2.tar.gz
10.3,进入nginx解压的目录下

cd nginx-1.15.2/
10.4,加入模块命令配置

./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/usr/local/fastdfs-nginx-module-1.20/src
10.5,编译并安装

make && make install
10.6,修改nginx配置

cd /opt/nginx/conf
vim nginx.conf
在这里插入图片描述

10.7,启动nginx

cd /usr/bin/
./nginx #启动


11、在浏览器中访问上传到fastDFS的图片
因为Centos系统有防火墙,需要先关闭掉,才可以在浏览器中访问;
(1)CentOS 7.0默认使用的是firewall作为防火墙;若没有启用iptables 作为防火墙,则使用以下方式关闭防火墙:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
(2)若已经启用iptables作为防火墙,则使用以下方式关闭:
service iptables stop #临时关闭防火墙
chkconfig iptables off #永久关闭防火墙
(3)在谷歌浏览器中访问刚才上传的图片:
**

运行了nginx就可以对图片浏览器下载?

不然只能上传?

**
**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值