Deploy kkfileviewer with Docker and Integrate MinIO in a Spring Boot Application
This guide walks through installing the kkfileviewer container using Docker, building and running its image, then shows how to add MinIO support to a Spring Boot project by configuring dependencies, creating configuration and utility classes, and exposing upload and preview endpoints.
1. Install kkfileviewer Container
1.1 Download Files
Use the kkfileviewer 4.4.0‑beta version; clone the repository from https://codeup.aliyun.com/6254dee9a923b68581caaf50/kkfileviewer.git .
1.2 Build Image
git clone https://codeup.aliyun.com/6254dee9a923b68581caaf50/kkfileviewer.git
cd kkfileviewer
docker build -t kkfileview:v4.4.0 .1.3 Run kkfileviewer
docker run -d -p 8012:8012 --name kkfileview kkfileview:v4.4.01.4 Test Access
Open http://your-ip:8012 in a browser to verify the service.
2. Spring Boot Integration with MinIO
2.1 Add Dependency
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.11</version>
</dependency>2.2 Configuration
# minio file storage configuration
minio:
endpoint: http://xxxxx:9000
accessKey: xxxx
secretKey: xxxxx
bucketName: test2.3 MinIO Configuration Class
package com.sunny.config;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MinioConfig {
@Value("${minio.endpoint}") private String endPoint;
@Value("${minio.accessKey}") private String accessKey;
@Value("${minio.secretKey}") private String secretKey;
@Value("${minio.bucketName}") private String bucketName;
@Bean
protected MinioClient minioClient() {
return MinioClient.builder()
.endpoint(endPoint)
.credentials(accessKey, secretKey)
.build();
}
}2.4 MinIO Utility Class
package com.sunny.utils;
import com.sunny.entity.common.ApiResult;
import com.sunny.exception.AppException;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.http.Method;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
@Component
public class MinioUtils {
@Value("${minio.bucketName}") private String bucketName;
@Resource private MinioClient minioClient;
public ApiResult uploadFile(MultipartFile file) throws AppException {
String fileName = System.currentTimeMillis() + file.getOriginalFilename();
try (InputStream fi = file.getInputStream()) {
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(bucketName)
.contentType(file.getContentType())
.object(fileName)
.stream(fi, fi.available(), -1)
.build();
minioClient.putObject(putObjectArgs);
} catch (Exception e) {
throw new AppException("文件上传失败" + e.getMessage());
}
return ApiResult.ok(fileName);
}
public ApiResult getPreviewUrl(String objectName) throws AppException {
try {
GetPresignedObjectUrlArgs urlArgs = GetPresignedObjectUrlArgs.builder()
.bucket(bucketName)
.object(objectName)
.method(Method.GET)
.build();
return ApiResult.ok(minioClient.getPresignedObjectUrl(urlArgs));
} catch (Exception e) {
throw new AppException("获取预览链接失败" + e.getMessage());
}
}
}2.5 Controller Endpoints
package com.sunny.controller;
import com.sunny.entity.common.ApiResult;
import com.sunny.exception.AppException;
import com.sunny.utils.MinioUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/file")
public class FileOperationController {
@Resource private MinioUtils minioUtils;
@PostMapping("/upload")
public ApiResult upload(MultipartFile file) throws AppException {
return minioUtils.uploadFile(file);
}
@GetMapping("/getPreviewUrl")
public ApiResult getPreviewUrl(String fileName) throws AppException {
return minioUtils.getPreviewUrl(fileName);
}
}3. Testing
3.1 Upload File
Use the /file/upload endpoint to upload a file; the response returns a unique filename stored in MinIO.
3.2 Get MinIO Preview URL
Call /file/getPreviewUrl?fileName=yourFileName to obtain a presigned URL for previewing the file.
3.3 File Preview
Insert the obtained URL into the kkfileviewer service to view the file directly in the browser.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.