Jfinal 中 MinIo文件服务器 完整配置以及代码实现
工具类文件 MinIoUtils 以及初始化方法
package com.hegao.pdos.core.uitle;
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.InputStream;
public class MinIoUtils {
private static MinioClient instance;
public void init() {
try {
instance = new MinioClient("http://192.168.3.39",9000, "minioadmin", "minioadmin");
} catch (InvalidPortException e) {
e.printStackTrace();
} catch (InvalidEndpointException e) {
e.printStackTrace();
}
}
/**
* 判断 bucket是否存在
*
* @param bucketName
* @return
*/
public static boolean bucketExists(String bucketName) {
try {
return instance.bucketExists(bucketName);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 创建 bucket
*
* @param bucketName
*/
public static void makeBucket(String bucketName) {
try {
boolean isExist = instance.bucketExists(bucketName);
if (!isExist) {
instance.makeBucket(bucketName);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件上传
*
* @param bucketName 捅名
* @param objectName 文件名
* @param filename 文件地址
*/
public static String putObject(String bucketName, String objectName, String filename) {
try {
instance.putObject(bucketName, objectName, filename);
return instance.getObjectUrl(bucketName, objectName);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 文件上传
*
* @param bucketName
* @param objectName
* @param stream
* @param contentType
* @return
*/
public static String putObject(String bucketName, String objectName, InputStream stream, String contentType) {
try {
instance.putObject(bucketName, objectName, stream, contentType);
return instance.getObjectUrl(bucketName, objectName);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 删除文件
*
* @param bucketName
* @param objectName
*/
public static void removeObject(String bucketName, String objectName) {
try {
instance.removeObject(bucketName, objectName);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
MinIoUtils minIoUtils = new MinIoUtils();
minIoUtils.init();
Boolean b = MinIoUtils.bucketExists("member");
System.out.println(b);
}
}
配置文件 JFinal configPlugin()配置
在该方法中启动文件服务器初始化
@Override
public void configPlugin(Plugins me) {
//配置文件服务器
new MinIoUtils().init();
}
接口调用 XXXController
public void testSend() {
Boolean b = MinIoUtils.bucketExists("member");
System.out.println(b);
rendJson(b);
}