# 功能描述

实现文件上传、下载、查看等功能。
配置文件为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: /Users/xiong/Desktop/temp/att/  #正式文件目录
      temp-path: /Users/xiong/Desktop/temp/temp/  #临时文件目录

#######  文件大小 配置 ########
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

# Service接口

# 第一步 引入包

<dependency>
    <groupId>sei-cloud</groupId>
    <artifactId>file</artifactId>
</dependency>
1
2
3
4

# 第二步 引入接口

@Resource
    FileService fileService;
1
2

# 第三步 使用接口

/**
 *
 * @author xiong
 */
public interface FileService {

    /**
     * 单文件上传
     * @param module: 模块编号
     * @param docId: 所属数据行的主键值(用于确定二级目录)
     * @param file: 文件内容
     * @param fileName: 保存的文件名,为null则使用上传文件的文件名
     * @param isRename: 是否需要更名
     * @param savePath: 保存的路径,为null则使用用户根目录
     * @return [失败和成功的文件列表]
     */
    ResMsg upload(String module, String docId, @RequestParam("file") MultipartFile file, @Nullable String fileName, boolean isRename, @Nullable String savePath);

    /**
     * 多文件上传
     * @param module: 模块编号
     * @param docId: 所属数据行的主键值(用于确定二级目录)
     * @param path: 保存的路径
     * @param isRename: 是否需要更名
     * @return ResMsg
     */
    ResMsg multiUpload(String module, String docId, @NonNull String path, boolean isRename);

    /**
     * 删除文件
     * @param module: 模块编号
     * @param docId: 所属数据行的主键值(用于确定二级目录)
     * @param filePathName: 要删除的文件
     * @return ResMsg
     * @throws IOException 异常
     */
    ResMsg delete(String module, String docId, @NonNull String filePathName) throws IOException;

    /**
     * 下载文件
     * @param module: 模块编号
     * @param docId: 所属数据行的主键值(用于确定二级目录)
     * @param response: response
     * @param filePathName: 文件路径及名称
     * @throws IOException 异常
     */
    void down(String module, String docId, HttpServletResponse response, String filePathName) throws IOException;

    /**
     * 查看文件
     * @param module: 模块编号
     * @param docId: 所属数据行的主键值(用于确定二级目录)
     * @param response: response
     * @param pathFileName: 文件路径及名称
     */
    void see(String module, String docId, HttpServletResponse response, @NonNull 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

# pdf转换接口

# 第一步 引入包

<dependency>
    <groupId>sei-cloud</groupId>
    <artifactId>file</artifactId>
</dependency>
1
2
3
4

# 第二步 引入接口

@Resource
    FileService fileService;
1
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

# Controller接口

@Api(description = "文件服务")
@ConditionalOnProperty(name = "sei.cloud.file.enabled", havingValue = "true")
@RequestMapping("/api/file")
public class FileController {

    @ApiOperation(value = "单文件上传服务(公共接口)")
    @RequestMapping(value="/public/upload", method = RequestMethod.POST,produces = { MediaType.APPLICATION_JSON_VALUE })
    public @ResponseBody ResMsg publicUpload(@ApiParam(value = "MultipartFile文件", required = true) @RequestParam("file") MultipartFile file, String filename, String module, String action, String key, boolean isRename) throws Exception;

    @ApiOperation(value = "多文件上传服务(公共接口)")
    @RequestMapping(value="/public/multiUpload", method = RequestMethod.POST)
    public @ResponseBody ResMsg publicMultiUpload(String module, String action, String key, boolean isRename) 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 = "/see/public/{module}/{key}/{filename}",method = {RequestMethod.GET})
    public void publicSee(@PathVariable(name="module") String module, @PathVariable(name="key") String key,@PathVariable(name="filename") String filename, HttpServletResponse response) 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