继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

【九月打卡】第17天 FastDFS开发

weishiba
关注TA
已关注
手记 21
粉丝 1
获赞 4

一、课程介绍

【课程名称】SpringBoot 2.x 实战仿B站高性能后端项目。
【章节】第四章 4.5 SpringBoot整合FastDFS
【讲师】HELLOSTAR

二、课程内容

1.引入依赖和配置文件

pom文件中引入FastDFS依赖。

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

配置文件中配置Tracker服务器地址:
fdfs.tracker-list=39.107.54.180:22122

2.使用FastDFS实现文件基础方法:文件上传下载

FastFileStorageClient接口是客户端与服务端交互的官方工具接口,封装了基本的处理文件的功能。

FastFile是FastDFS中的基本文件类型,字段包括二进制形式存储的文件,文件大小,文件后缀名,文件属性,所在分组。文件的处理等功能围绕这些字段进行。

public class FastFile {
    protected InputStream inputStream;
    protected long fileSize;
    protected String fileExtName;
    protected Set<MetaData> metaDataSet;
    protected String groupName;
}

MultipartFile是Spring框架封装的存储文件的接口。使用二进制形式存储识别文件类型。存储了文件的各种基本信息。

获取文件后缀名功能

 public String getFileType(MultipartFile file){
        if(file == null){
            throw new ConditionException("非法文件!");
        }
        String fileName = file.getOriginalFilename();
        int index = fileName.lastIndexOf(".");
        return fileName.substring(index+1);
    }

上传文件功能
实现将文件上传到存储服务器,并且返回的是文件存储的路径信息。

//上传
    public String uploadCommonFile(MultipartFile file) throws Exception {
        Set<MetaData> metaDataSet = new HashSet<>();
        String fileType = this.getFileType(file);
        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(),
								  fileType, metaDataSet);
        return storePath.getPath();
    }

三、课程收获

使用FastDFS和Spring框架搭配使用进行文件类型的处理操作。使用框架封装的方法和接口快速处理文件类型。

四、学习过程

开发记录
图片描述

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP