FastDFS与Springboot集成

释放双眼,带上耳机,听听看~!

整合到Springboot项目流程
注意:必须是Springboot项目

1、添加pom依赖


1
2
3
4
5
6
1<dependency>
2    <groupId>com.github.tobato</groupId>
3    <artifactId>fastdfs-client</artifactId>
4    <version>1.25.2-RELEASE</version>
5</dependency
6

2、将Fdfs配置引入项目
我将注解配置加在springboot的入口类中:@Import(FdfsClientConfig.class)


1
2
3
4
5
6
7
8
9
1@Import(FdfsClientConfig.class)
2@SpringBootApplication
3public class JingtongApplication {
4
5    public static void main(String[] args) {
6        SpringApplication.run(JingtongApplication.class, args);
7    }
8}
9

3、在spring配置文件中加入fdfs相关配置
根据项目当中使用配置文件类型(.yml和.properties选择其中一个),加入相应的配置。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1application.yml
2
3fdfs:
4  soTimeout: 1500
5  connectTimeout: 600
6  thumbImage:             #缩略图生成参数
7    width: 150
8    height: 150
9  trackerList:            #TrackerList参数,支持多个
10    - 192.168.0.201:22122
11    - 192.168.0.202:22122 
12
13
14
15
16
17application.properties
18
19fdfs.soTimeout=1500
20fdfs.connectTimeout=600
21fdfs.thumbImage.width=150
22fdfs.thumbImage.height=150
23fdfs.trackerList[0]=192.168.0.201:22122
24fdfs.trackerList[1]=192.168.0.202:22122
25

4、在项目中使用
客户端主要包括以下接口:
TrackerClient – TrackerServer接口
GenerateStorageClient – 一般文件存储接口 (StorageServer接口)
FastFileStorageClient – 为方便项目开发集成的简单接口(StorageServer接口)
AppendFileStorageClient – 支持文件续传操作的接口 (StorageServer接口)

笔者在前一个项目当中将fdfs主要用于图片存储,基于FastFileStorageClient接口和springmvc提供的MultipartFile接口封装了一个简单的工具类,方便全局管理与调用。如下所示:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
1package com.digi_zones.support.fs;
2
3import com.digi_zones.config.AppConfig;
4import com.digi_zones.contacts.AppConstants;
5import com.github.tobato.fastdfs.domain.FileInfo;
6import com.github.tobato.fastdfs.domain.StorePath;
7import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
8import com.github.tobato.fastdfs.service.FastFileStorageClient;
9import org.apache.commons.io.FilenameUtils;
10import org.apache.commons.lang3.StringUtils;
11import org.slf4j.Logger;
12import org.slf4j.LoggerFactory;
13import org.springframework.beans.factory.annotation.Autowired;
14import org.springframework.stereotype.Component;
15import org.springframework.web.multipart.MultipartFile;
16
17import java.io.ByteArrayInputStream;
18import java.io.IOException;
19import java.nio.charset.Charset;
20
21/**
22 * <p>Description: FastDFS文件上传下载包装类</p>
23 * <p>Copyright: Copyright (c) 2016</p>
24 *
25 * @author 杨信
26 * @version 1.0
27 * @date 2016/9/7
28 */
29@Component
30public class FastDFSClientWrapper {
31
32    private final Logger logger = LoggerFactory.getLogger(FastDFSClientWrapper.class);
33
34    @Autowired
35    private FastFileStorageClient storageClient;
36
37    @Autowired
38    private AppConfig appConfig;   // 项目参数配置
39
40    /**
41     * 上传文件
42     * @param file 文件对象
43     * @return 文件访问地址
44     * @throws IOException
45     */
46    public String uploadFile(MultipartFile file) throws IOException {
47        StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
48        return getResAccessUrl(storePath);
49    }
50
51    /**
52     * 将一段字符串生成一个文件上传
53     * @param content 文件内容
54     * @param fileExtension
55     * @return
56     */
57    public String uploadFile(String content, String fileExtension) {
58        byte[] buff = content.getBytes(Charset.forName("UTF-8"));
59        ByteArrayInputStream stream = new ByteArrayInputStream(buff);
60        StorePath storePath = storageClient.uploadFile(stream,buff.length, fileExtension,null);
61        return getResAccessUrl(storePath);
62    }
63
64    // 封装图片完整URL地址
65    private String getResAccessUrl(StorePath storePath) {
66        String fileUrl = AppConstants.HTTP_PRODOCOL + appConfig.getResHost()
67                + ":" + appConfig.getFdfsStoragePort() + "/" + storePath.getFullPath();
68        return fileUrl;
69    }
70
71    /**
72     * 删除文件
73     * @param fileUrl 文件访问地址
74     * @return
75     */
76    public void deleteFile(String fileUrl) {
77        if (StringUtils.isEmpty(fileUrl)) {
78            return;
79        }
80        try {
81            StorePath storePath = StorePath.praseFromUrl(fileUrl);
82            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
83        } catch (FdfsUnsupportStorePathException e) {
84            logger.warn(e.getMessage());
85        }
86    }
87}
88

除了FastDFSClientWrapper类中用到的api,客户端提供的api还有很多,可根据自身的业务需求,将其它接口也添加到工具类中即可。如下所示:

// 上传文件,并添加文件元数据
StorePath uploadFile(InputStream inputStream, long fileSize, String fileExtName, Set<MateData> metaDataSet);
// 获取文件元数据
Set<MateData> getMetadata(String groupName, String path);
// 上传图片并同时生成一个缩略图
StorePath uploadImageAndCrtThumbImage(InputStream inputStream, long fileSize, String fileExtName,
Set<MateData> metaDataSet);
// 。。。
在项目中使用FastDFSClientWrapper:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1@Controller
2public class MyController {
3
4    @Autowired
5    private FastDFSClientWrapper dfsClient;
6
7    // 上传图片
8    @RequestMapping(value = &quot;/upload&quot;, method = RequestMethod.POST)
9    public String upload(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception {
10        // 省略业务逻辑代码。。。
11        String imgUrl = dfsClient.uploadFile(file);
12        // 。。。。
13        return xxxx;
14
15    }
16}
17

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

c++ list, vector, map, set 区别与用法比较

2022-1-11 12:36:11

安全资讯

开发者在2017至2019年提交的应用 有约35%被苹果App Store打回

2021-8-16 15:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索