Spring Boot搭配MinIO和KKFileView實現(xiàn)文件存儲和在線預覽
在現(xiàn)代的 Web 應用中,文件上傳和預覽是常見的需求場景。尤其是在內(nèi)容管理系統(tǒng)(CMS)或企業(yè)內(nèi)部應用中,文件預覽功能尤為重要。通過 Spring Boot 搭配 MinIO 和 KKFileView,我們可以輕松實現(xiàn)高效的文件存儲和在線預覽功能。
本文將從以下幾個方面,帶你逐步實現(xiàn)一個簡單的文件預覽系統(tǒng)。
一、項目背景和技術(shù)選型
- Spring Boot:主流的 Java 后端開發(fā)框架,用于快速構(gòu)建 RESTful 服務。
- MinIO:一款高性能的對象存儲服務,支持 S3 協(xié)議,適合大文件存儲。
- KKFileView:一款輕量級文件在線預覽服務,支持多種文件格式(如 Office 文檔、PDF、圖片等)。
系統(tǒng)架構(gòu)圖:
功能目標
- 用戶上傳文件到 MinIO。
- 后端通過 KKFileView 生成文件預覽鏈接。
- 用戶通過前端直接查看文件內(nèi)容。
二、環(huán)境準備
1. MinIO 安裝和啟動
下載并運行 MinIO:
docker run -p 9000:9000 -p 9001:9001 \ --name minio \ -e "MINIO_ROOT_USER=minioadmin" \ -e "MINIO_ROOT_PASSWORD=minioadmin" \ quay.io/minio/minio server /data --console-address ":9001"
訪問 MinIO 控制臺:http://localhost:9001
默認賬號密碼:
- 用戶名:minioadmin
- 密碼:minioadmin
創(chuàng)建一個存儲桶(如 preview-files)。
2. KKFileView 安裝和啟動
下載并運行 KKFileView:
docker run -d --name kkfileview \ -p 8012:8012 \ --restart=always \ kekingcn/kkfileview:latest
訪問 KKFileView 服務:http://localhost:8012
三、項目代碼實現(xiàn)
1. 引入依賴
在 pom.xml 文件中添加以下依賴:
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MinIO 客戶端 -->
<dependency>
<groupId>io.minio</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. 配置文件
在 application.yml 中配置 MinIO 和 KKFileView 服務地址:
minio: endpoint: http://localhost:9000 accessKey: minioadmin secretKey: minioadmin bucketName: preview-files kkfileview: serverUrl: http://localhost:8012
3. MinIO 文件上傳和預覽服務
創(chuàng)建 MinioService 類:
import io.minio.*;
import io.minio.errors.MinioException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.InputStream;
@Service
public class MinioService {
@Value("${minio.endpoint}")
private String endpoint;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Value("${minio.bucketName}")
private String bucketName;
private MinioClient getClient() {
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}
public String uploadFile(String fileName, InputStream inputStream, String contentType) throws Exception {
MinioClient client = getClient();
client.putObject(
PutObjectArgs.builder()
.bucket(bucketName)
.object(fileName)
.stream(inputStream, -1, 10485760)
.contentType(contentType)
.build()
);
return endpoint + "/" + bucketName + "/" + fileName;
}
}4. KKFileView 服務對接
創(chuàng)建 FilePreviewService 類:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class FilePreviewService {
@Value("${kkfileview.serverUrl}")
private String kkFileViewServerUrl;
public String generatePreviewUrl(String fileUrl) {
return kkFileViewServerUrl + "/onlinePreview?url=" + fileUrl;
}
}5. REST 控制器
創(chuàng)建 FileController 類:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
@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 = file.getInputStream()) {
String fileUrl = minioService.uploadFile(file.getOriginalFilename(), inputStream, file.getContentType());
return "上傳成功,文件地址:" + fileUrl;
} catch (Exception e) {
e.printStackTrace();
return "上傳失?。? + e.getMessage();
}
}
@GetMapping("/preview")
public String previewFile(@RequestParam("fileUrl") String fileUrl) {
return filePreviewService.generatePreviewUrl(fileUrl);
}
}四、運行和測試
- 啟動 Spring Boot 項目。
- 上傳文件:
curl -F "file=@example.pdf" http://localhost:8080/files/upload
輸出類似以下內(nèi)容:
上傳成功,文件地址:http://localhost:9000/preview-files/example.pdf
- 生成預覽鏈接:
curl "http://localhost:8080/files/preview?fileUrl=http://localhost:9000/preview-files/example.pdf"
輸出類似以下內(nèi)容:
http://localhost:8012/onlinePreview?url=http://localhost:9000/preview-files/example.pdf
- 在瀏覽器中打開預覽鏈接即可查看文件內(nèi)容。
五、總結(jié)
通過 Spring Boot、MinIO 和 KKFileView 的結(jié)合,我們輕松實現(xiàn)了文件上傳和在線預覽功能。
- MinIO 提供了高效的對象存儲服務。
- KKFileView 提供了強大的文檔解析能力。
- Spring Boot 實現(xiàn)了兩者的無縫對接。
這套方案輕量高效,適合企業(yè)內(nèi)部文件管理系統(tǒng),也可以輕松擴展至分布式環(huán)境。
到此這篇關(guān)于Spring Boot搭配MinIO和KKFileView實現(xiàn)文件存儲和在線預覽的文章就介紹到這了,更多相關(guān)Spring Boot實現(xiàn)文件上傳和預覽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
簡單了解java函數(shù)式編碼結(jié)構(gòu)及優(yōu)勢
這篇文章主要介紹了簡單了解java函數(shù)式編碼結(jié)構(gòu)及優(yōu)勢,本文將探討三種下一代 JVM 語言:Groovy、Scala 和 Clojure,比較并對比新的功能和范例,讓 Java 開發(fā)人員對自己近期的未來發(fā)展有大體的認識。,需要的朋友可以參考下2019-06-06
thymeleaf中前后端數(shù)據(jù)交互方法匯總
這篇文章主要介紹了thymeleaf中前后端數(shù)據(jù)交互小結(jié),本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2022-07-07
SpringBoot中的靜態(tài)資源訪問的實現(xiàn)
這篇文章主要介紹了SpringBoot中的靜態(tài)資源訪問的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
Spring多線程通過@Scheduled實現(xiàn)定時任務
這篇文章主要介紹了Spring多線程通過@Scheduled實現(xiàn)定時任務,@Scheduled?定時任務調(diào)度注解,是spring定時任務中最重要的,下文關(guān)于其具體介紹,需要的小伙伴可以參考一下2022-05-05

