SpringBoot+minio+kkfile實現(xiàn)文件預(yù)覽功能
1、容器安裝kkfileviewer
1.1 下載文件
這里以kkfile 4.4.0-beta版本為例
下載kkfile安裝包及Dockerfile:
https://codeup.aliyun.com/6254dee9a923b68581caaf50/kkfileviewer.git
1.2、構(gòu)建鏡像
git clone https://codeup.aliyun.com/6254dee9a923b68581caaf50/kkfileviewer.git cd kkfileviewer docker build -t kkfileview:v4.4.0 .
1.3、 啟動kkfileviewer
docker run -d -p 8012:8012 --name kkfileview kkfileview:v4.4.0
1.4、 訪問測試
http://you-ip:8012

2、springboot集成minio
2.1 pom.xml添加依賴
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.11</version>
</dependency>2.2、 配置
# minio 文件存儲配置信息 minio: endpoint: http://xxxxx:9000 accessKey: xxxx secretKey: xxxxx bucketName: test
2.3、minio配置類
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工具類
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("獲取預(yù)覽鏈接失敗" + e.getMessage());
}
}
}2.5、接口
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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、測試
3.1、上傳文件

3.2、獲取minio文件預(yù)覽地址
3.1中返回一個文件名,該文件名為上傳文件在minio中的唯一名稱,使用該名稱請求minio文件預(yù)覽地址

3.3、文件預(yù)覽
3.2中的接口返回一個地址,將地址放到kkfileviewer文件預(yù)覽服務(wù)中,可以預(yù)覽文件

以上就是SpringBoot+minio+kkfile實現(xiàn)文件預(yù)覽功能的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot minio kkfile文件預(yù)覽的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決Idea的選擇文件后定位瞄準(zhǔn)器"Select Opened File"的功能
使用IntelliJ IDEA時,可能會發(fā)現(xiàn)"SelectOpenedFile"功能不見了,這個功能允許用戶快速定位到當(dāng)前打開文件的位置,若要找回此功能,只需在IDEA的標(biāo)題欄上右鍵,然后選擇"Always Select Opened File",這樣就可以重新啟用這個便捷的功能2024-11-11
springmvc實現(xiàn)json交互-requestBody和responseBody
本文主要介紹了springmvc實現(xiàn)json交互-requestBody和responseBody的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
java網(wǎng)絡(luò)之基于UDP的聊天程序示例解析
這篇文章主要介紹了java網(wǎng)絡(luò)之基于UDP的聊天程序示例解析,文中通過步驟及示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Java多線程通訊之wait,notify的區(qū)別詳解
這篇文章主要介紹了Java多線程通訊之wait,notify的區(qū)別詳解,非常不錯,具有一定的參考借鑒借鑒價值,需要的朋友可以參考下2018-07-07
IDEA配置使用Maven Helper插件的方法(詳細(xì)配置)
這篇文章主要介紹了Maven Helper插件IDEA配置使用(詳細(xì)配置),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12

