SpringBoot2 integrates FastDFS middleware for distributed file management

Keywords: Programming github Spring SpringBoot

Source code for this article: GitHub. Click here || GitEE. Click here

1. Introduction to FastDFS

1. Function of FastDFS

FastDFS is an open source, lightweight, distributed file system that manages files, including file storage, file synchronization, file upload, file download, and so on. It solves the problems of mass storage and load balancing.

Install connection:

Detailed installation process

2. Core Role

FastDFS consists of a trackerserver, a storage server, and a client.

1) Tracking Server

The coordinator of FastDFS, who manages all storage servers and groups, connects each store to the Tracker after it starts, informs him of the group he belongs to, and maintains a periodic heart rate. Based on the heart rate information of the store, the tracker establishes a map of the group to the [storage server list].

2) Storage Server

As a group, a group contains more than one storage machine, data is backed up each other, and storage space is based on the smallest storage volume in the group. Therefore, it is recommended that multiple storage within the group be configured as equally as possible to avoid wasting storage space.

3) Client

The originator of a business request uses the TCP/IP protocol to interact with the tracker server or storage node through a proprietary interface.

3. Operation process

1. Store service to upload status information to tracking service regularly;
2. The client initiates the request;
3. Tracker synchronizes memory state, returns storage service port and IP;
4. Clients perform file operations (upload, download), etc.

Integration with SpringBoot2

1. Core Steps

1) Configure FastDFS execution environment
 2) File upload configuration
 3) Integrate Swagger2 test interface

2. Core Dependency

<!-- FastDFS rely on -->
<!-- Swagger2 Core Dependency -->

3. Configure FastDFS

0) Core Profile

  # Link Timeout
  connect-timeout: 60
  # Read Time
  so-timeout: 60
  # Generate thumbnail parameters
    width: 150
    height: 150

1) Core Configuration Class

// Problems with Jmx duplicate registration bean s
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class DfsConfig {

2) File Tool Class

public class FileDfsUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileDfsUtil.class);
    private FastFileStorageClient storageClient ;
     * Upload Files
    public String upload(MultipartFile multipartFile) throws Exception{
        String originalFilename = multipartFile.getOriginalFilename().
                                  lastIndexOf(".") + 1);
        StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
                              multipartFile.getSize(),originalFilename , null);
        return storePath.getFullPath() ;
     * Delete Files
    public void deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            LOGGER.info("fileUrl == >>File path is empty...");
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (Exception e) {

4. File upload configuration

    name: ware-fast-dfs
      enabled: true
      max-file-size: 10MB
      max-request-size: 20MB

5. Configure Swagger2

It is mainly used to generate a test interface for file upload.

1) Configuration code class

public class SwaggerConfig {
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot utilize Swagger structure API File")
                .description("Use RestFul style, Creator: Smiled")
                .version("version 1.0")

2) Start class annotations


3. Demonstration Cases

1. Interface Code

public class FileController {
    private FileDfsUtil fileDfsUtil ;
     * File Upload
    @ApiOperation(value="Upload Files", notes="test FastDFS File Upload")
    @RequestMapping(value = "/uploadFile",headers="content-type=multipart/form-data", method = RequestMethod.POST)
    public ResponseEntity<String> uploadFile (@RequestParam("file") MultipartFile file){
        String result ;
            String path = fileDfsUtil.upload(file) ;
            if (!StringUtils.isEmpty(path)){
                result = path ;
            } else {
                result = "Upload failed" ;
        } catch (Exception e){
            e.printStackTrace() ;
            result = "Service Exception" ;
        return ResponseEntity.ok(result);
     * File Delete
    @RequestMapping(value = "/deleteByPath", method = RequestMethod.GET)
    public ResponseEntity<String> deleteByPath (){
        String filePathName = "group1/M00/00/00/wKhIgl0n4AKABxQEABhlMYw_3Lo825.png" ;
        return ResponseEntity.ok("SUCCESS") ;

2. Execution process

1. Access the http://localhost:7010/swagger-ui.html test interface
 2. Call the file upload interface to get the file path in the FastDFS service
 3. Browser access:
 4. Call Delete Interface, Delete Pictures on Server
 5. Empty the browser cache, access the picture Url again, return 404

4. Source code address

GitHub Address: Know a smile
 Code Cloud Address: Know a smile

Posted by faraco on Mon, 02 Sep 2019 17:35:44 -0700