# 功能描述
实现文件上传、下载、查看等功能。
配置文件为application-file.yml
提示
(1) 需在application.yml中增加spring.profiles.active中增加file才能激活配置文件
(2) 需设置application-file.yml中的sei.cloud.file.enabled为true才能开启file服务
# 配置一览表
sei:
cloud:
file: #附件存放路径
enabled: true #设置为true才开启文件上传服务
root-path: (APP_PATH)/file/ #正式文件目录绝对路径或带变量的路径, (APP_PATH) 为变量,代表当前项目的目录
temp-path: (APP_PATH)/temp/ #临时文件目录绝对路径或带变量的路径, (APP_PATH) 为变量,代表当前项目的目录
oss:
aliYun: #阿里云OSS
expireTime: 3000 #token过期时间(秒),阿里云默认为3600秒
region: "oss-cn-beijing" #上传文件使用
regionId: "cn-beijing"
accessKeyId: ""
secret: ""
roleArn: ""
roleSessionName: ""
bucket: ""
endpoint: ""
####### 文件大小 配置 ########
spring:
servlet:
multipart:
enabled: true #是否支持批量上传,默认为true
file-size-threshold: 0 #当大于这个阈值时将写入磁盘,否则在内存中,默认为0,一般情况下不用修改
max-file-size: 100MB #单个文件的大小上限,设置为-1不限制,默认为1MB
max-request-size: 100MB #单个请求的文件总大小上限,设置为-1不限制,默认为10MB
####### word等转pdf 配置 ########
jodconverter:
local:
enabled: false #设置为true才开启服务
office-home: /Applications/LibreOffice.app/Contents/ #执行文件安装目录: Linux:/opt/libreoffice6.3/ Windows:C:/Program Files (x86)/OpenOffice 4/
max-tasks-per-process: 10
port-numbers: 4100
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
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
# Service接口
# 第一步 引入包
<dependency>
<groupId>sei-cloud</groupId>
<artifactId>file</artifactId>
</dependency>
1
2
3
4
2
3
4
# 第二步 引入接口
@Resource
FileService fileService;
1
2
2
# 第三步 使用接口
/**
* 文件上传下载服务
* @author xiong
*/
public interface FileService {
/**
* 获得阿里云的token
* @return Object
*/
Map<Object, Object> getAliYunOssToken();
/**
* 获得minio的token
* @param mode: 写入模式:write, 读取模式:read
* @return
*/
JSONObject getMinioOssToken(String mode);
/**
* 单文件上传
* @param file: 文件
* @param path: 存放的绝对路径
* @param newFileName: 改名的新文件名,为null则使用上传文件的文件名
* @param data: 附带数据
* @return [失败和成功的文件列表]
*/
ResMsg upload(MultipartFile file, String path, String newFileName, Map<String, Object> data);
/**
* 将文件传输到阿里云
* @param ossConfig: 【可选】阿里云配置,为空则使用配置文件的选项
* @param saveFilePathName: 存放到阿里云的路径和文件名
* @param filePathName: 要传输的文件绝对路径和文件名列表
* @throws Exception
*/
void uploadToAliYun(@Nullable JSONObject ossConfig, @NonNull String saveFilePathName, @NonNull String... filePathName);
/**
* 删除阿里云目录
* @param ossConfig: 【可选】阿里云配置,为空则使用配置文件的选项
* @param dir: 要删除的目录名
*/
void deleteDirectoriesAliYun(@Nullable JSONObject ossConfig, String dir);
/**
* 删除阿里云文件
* @param ossConfig: 【可选】阿里云配置,为空则使用配置文件的选项
* @param filePathName: 要删除的 文件名
* @throws Exception
*/
void deleteFromAliYun(@Nullable JSONObject ossConfig, @NonNull String... filePathName) throws Exception;
/**
* 多文件上传(如需对上传文件改名则设置map中的fileNameMap对象,类型为Map,key为原始文件名,value为新文件名)
* @param files: 文件列表
* @param path: 存储绝对路径
* @param data: 附带数据
* @return ResMsg
*/
ResMsg multiUpload(List<MultipartFile> files, String path, Map<String, Object> data);
/**
* 删除文件
* @param pathFileName: 要删除的文件绝对路径和文件名
* @param data: 附带数据
* @return ResMsg
* @throws IOException 异常
*/
ResMsg delete(String pathFileName, JSONObject data) throws IOException;
/**
* 下载文件
* @param pathFileName: 要下载的文件文件绝对路径和文件名
* @param response: response
* @param data: 附带数据
* @throws IOException 异常
*/
void down(String pathFileName, HttpServletResponse response, JSONObject data) throws IOException;
/**
* 在线浏览文件
* @param pathFileName: 要浏览的文件绝对路径和文件名
* @param response: response
* @param data: 附带数据
*/
void see(String pathFileName, HttpServletResponse response, Map<String, Object> data);
}
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
89
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
89
# pdf转换接口
# 第一步 引入包
<dependency>
<groupId>sei-cloud</groupId>
<artifactId>file</artifactId>
</dependency>
1
2
3
4
2
3
4
# 第二步 引入接口
@Resource
FileService fileService;
1
2
2
# 第三步 使用接口
public interface PdfConvertService {
/**
* 获得转换实体
* @return DocumentConverter
*/
DocumentConverter getDocumentConverter();
/**
* 将目标文件转换为pdf文件
* @param srcPathFileName: 源文件
* @param targetPathFileName: 转换后的文件
* @throws OfficeException 异常
*/
void conver(@NonNull String srcPathFileName, @NonNull String targetPathFileName) throws OfficeException;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Controller接口
@Api(description = "文件服务")
@ConditionalOnProperty(name = "sei.cloud.file.enabled", havingValue = "true")
@RequestMapping("/api/file")
public class FileController {
@ApiOperation(value = "获取阿里云OSS的Token")
@RequestMapping(value="/oss/aliYun/getToken", method = RequestMethod.POST,produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody ResMsg getAliYunOssToken();
@ApiOperation(value = "单文件上传服务(公共接口)")
@RequestMapping(value="/public/upload", method = RequestMethod.POST,produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody ResMsg publicUpload(@RequestParam("file") MultipartFile file, @RequestParam Map<String, Object> data) throws Exception;
@ApiOperation(value = "多文件上传服务(公共接口)")
@RequestMapping(value="/public/multiUpload", method = RequestMethod.POST)
public @ResponseBody ResMsg publicMultiUpload(@RequestParam("file") List<MultipartFile> file, @RequestParam Map<String, Object> data) throws Exception;
@ApiOperation(value = "单文件删除服务(公共接口)")
@RequestMapping(value="/public/del", method = RequestMethod.POST,produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody ResMsg publicDel(@RequestBody JSONObject data) throws Exception;
@ApiOperation(value = "下载文件服务(公共接口)",response = ResMsg.class)
@RequestMapping(value = "/public/down",method = {RequestMethod.POST})
public void publicDown(@RequestBody JSONObject data, HttpServletResponse response) throws Exception;
@ApiOperation(value = "文件访问服务",response = ResMsg.class)
@RequestMapping(value = "/public/see/**",method = {RequestMethod.GET})
public void publicSee(HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String, Object> data) throws Exception;
}
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
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
# 事件接口
public abstract class FileEvent {
/**
* 单文件上传成功后回调
* @param resMsg: 返回前端的信息
* @param pathFileName: 上传的文件绝对路径和文件名
* @param map: 附带数据
*/
public void single_upload_after(ResMsg resMsg, String pathFileName, Map<String, Object> map) {
}
/**
* 多文件上传成功后回调
* @param resMsg: 返回前端的信息
* @param path: 存放绝对路径
* @param failList: 上传失败文件列表
* @param successList: 上传成功文件列表
* @param map: 附带数据
*/
public void multi_upload_after(ResMsg resMsg , String path, List<Map<String, String>> failList, List<Map<String, String>> successList, Map<String, Object> map) {
}
/**
* 删除文件前回调
* @param resMsg: 返回前端的信息
* @param pathFileName: 要删除的文件绝对路径和文件名
* @param data: 附带数据
* @return 返回true将删除,返回false将不删除,返回null终止删除并退出
*/
public Boolean delete_before(ResMsg resMsg, String pathFileName, JSONObject data) {
return true;
}
/**
* 删除文件后回调: 返回前端的信息
* @param resMsg: 返回前端的信息
* @param pathFileName: 要删除的文件绝对路径和文件名
* @param data: 附带数据
*/
public void delete_after(ResMsg resMsg, String pathFileName, JSONObject data) {
}
/**
* 下载文件前回调
* @param pathFileName: 要下载的文件绝路路径和文件名
* @param data: 附带数据
* @return 返回true允许下载,false不允许下载
*/
public boolean down_before(String pathFileName, JSONObject data) {
return true;
}
/**
* 下载文件后回调
* @param pathFileName: 要下载的文件绝路路径和文件名
* @param data: 附带数据
*/
public void down_after(String pathFileName, JSONObject data) {
}
/**
* 在线查看文件前回调
* @param pathFileName: 要浏览的文件绝对路径和文件名
* @return 返回true允许查看,false不允许查看
*/
public boolean see_before(String pathFileName) {
return true;
}
/**
* 在线查看文件后回调
* @param pathFileName: 要浏览的文件绝对路径和文件名
*/
public void see_after(String pathFileName) {
}
}
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
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