SpringBoot+minio+kkfile實(shí)現(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、 啟動(dòng)kkfileviewer
docker run -d -p 8012:8012 --name kkfileview kkfileview:v4.4.0
1.4、 訪問(wèn)測(cè)試
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 文件存儲(chǔ)配置信息 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、測(cè)試
3.1、上傳文件

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

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

以上就是SpringBoot+minio+kkfile實(shí)現(xiàn)文件預(yù)覽功能的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot minio kkfile文件預(yù)覽的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java實(shí)現(xiàn)簡(jiǎn)單銀行家算法
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單銀行家算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
java使用gzip實(shí)現(xiàn)文件解壓縮示例
這篇文章主要介紹了java使用gzip實(shí)現(xiàn)文件解壓縮示例,需要的朋友可以參考下2014-03-03
springboot整合shiro與自定義過(guò)濾器的全過(guò)程
這篇文章主要給大家介紹了關(guān)于springboot整合shiro與自定義過(guò)濾器以及Shiro中權(quán)限控制的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01
Spring攔截器之HandlerInterceptor使用方式
這篇文章主要介紹了Spring攔截器之HandlerInterceptor使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
使用maven打包/跳過(guò)某個(gè)modules
本文總結(jié)了在Maven項(xiàng)目中跳過(guò)或單獨(dú)構(gòu)建模塊的方法,包括使用`-pl`、`-am`和`-amd`參數(shù)來(lái)選擇性地執(zhí)行模塊構(gòu)建,以及通過(guò)`-Dmaven.test.skip`跳過(guò)測(cè)試,以提高構(gòu)建效率2024-12-12
SpringBoot攔截器如何獲取http請(qǐng)求參數(shù)
這篇文章主要給大家介紹了SpringBoot攔截器如何獲取http請(qǐng)求參數(shù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

