SoFunction
Updated on 2025-04-06

Spring Boot is paired with MinIO and KKFileView to achieve file storage and online preview

In modern web applications, file upload and preview are common demand scenarios. File preview is particularly important especially in content management systems (CMS) or internal enterprise applications. With Spring Boot with MinIO and KKFileView, we can easily achieve efficient file storage and online preview.

This article will take you to gradually implement a simple file preview system from the following aspects.

1. Project background and technical selection

  • Spring Boot: a mainstream Java backend development framework for quickly building RESTful services.
  • MinIO: A high-performance object storage service that supports S3 protocol and is suitable for large file storage.
  • KKFileView: A lightweight file online preview service that supports multiple file formats (such as Office documents, PDFs, pictures, etc.).

System architecture diagram:

Functional Objectives

  1. The user uploads the file to MinIO.
  2. The backend generates file preview links through KKFileView.
  3. Users view file contents directly through the front-end.

2. Environmental preparation

1. MinIO installation and startup

Download and run MinIO:

docker run -p 9000:9000 -p 9001:9001 \
  --name minio \
  -e "MINIO_ROOT_USER=minioadmin" \
  -e "MINIO_ROOT_PASSWORD=minioadmin" \
  /minio/minio server /data --console-address ":9001"

Access the MinIO console: http://localhost:9001

Default account password:

  • Username: miniioadmin
  • Password: miniioadmin

Create a bucket (such as preview-files).

2. KKFileView Installation and Start

Download and run KKFileView:

docker run -d --name kkfileview \
  -p 8012:8012 \
  --restart=always \
  kekingcn/kkfileview:latest

Access the KKFileView service: http://localhost:8012

3. Project code implementation

1. Introduce dependencies

Add the following dependencies to the file:

<dependencies>
    <!-- Spring Boot Web -->
    <dependency>
        <groupId></groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- MinIO Client -->
    <dependency>
        <groupId></groupId>
        <artifactId>minio</artifactId>
        <version>8.5.4</version>
    </dependency>
    <!-- Apache Commons IO -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
    </dependency>
</dependencies>

2. Configuration file

Configure the MinIO and KKFileView service addresses in

minio:
  endpoint: http://localhost:9000
  accessKey: minioadmin
  secretKey: minioadmin
  bucketName: preview-files

kkfileview:
  serverUrl: http://localhost:8012

3. MinIO file upload and preview service

Create the MinioService class:

import .*;
import ;
import ;
import ;

import ;

@Service
public class MinioService {

    @Value("${}")
    private String endpoint;

    @Value("${}")
    private String accessKey;

    @Value("${}")
    private String secretKey;

    @Value("${}")
    private String bucketName;

    private MinioClient getClient() {
        return ()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
    }

    public String uploadFile(String fileName, InputStream inputStream, String contentType) throws Exception {
        MinioClient client = getClient();
        (
                ()
                        .bucket(bucketName)
                        .object(fileName)
                        .stream(inputStream, -1, 10485760)
                        .contentType(contentType)
                        .build()
        );
        return endpoint + "/" + bucketName + "/" + fileName;
    }
}

4. KKFileView service docking

Create FilePreviewService class:

import ;
import ;

@Service
public class FilePreviewService {

    @Value("${}")
    private String kkFileViewServerUrl;

    public String generatePreviewUrl(String fileUrl) {
        return kkFileViewServerUrl + "/onlinePreview?url=" + fileUrl;
    }
}

5. REST controller

Create the FileController class:

import ;
import .*;
import ;

import ;

@RestController
@RequestMapping("/files")
public class FileController {

    @Autowired
    private MinioService minioService;

    @Autowired
    private FilePreviewService filePreviewService;

    @PostMapping("/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file) {
        try (InputStream inputStream = ()) {
            String fileUrl = ((), inputStream, ());
            return "Uploaded successfully, file address:" + fileUrl;
        } catch (Exception e) {
            ();
            return "Upload failed:" + ();
        }
    }

    @GetMapping("/preview")
    public String previewFile(@RequestParam("fileUrl") String fileUrl) {
        return (fileUrl);
    }
}

IV. Run and test

  1. Start the Spring Boot project.
  2. Upload file:
curl -F "file=@" http://localhost:8080/files/upload

The output is similar to the following:

Upload successfully,File address:http://localhost:9000/preview-files/
  1. Generate preview link:
curl "http://localhost:8080/files/preview?fileUrl=http://localhost:9000/preview-files/"

The output is similar to the following:

http://localhost:8012/onlinePreview?url=http://localhost:9000/preview-files/
  1. Open the preview link in your browser to view the file content.

5. Summary

With the combination of Spring Boot, MinIO and KKFileView, we easily implement file upload and online preview capabilities.

  • MinIO provides efficient object storage services.
  • KKFileView provides powerful document parsing capabilities.
  • Spring Boot achieves seamless connection between the two.

This solution is lightweight and efficient, suitable for internal file management systems of the enterprise, and can also be easily extended to a distributed environment.

This is the article about Spring Boot using MinIO and KKFileView to implement file storage and online preview. For more related Spring Boot to implement file upload and preview, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!