|
@@ -0,0 +1,322 @@
|
|
|
+package com.tofly.river.util;
|
|
|
+
|
|
|
+import com.tofly.river.common.Constants;
|
|
|
+import com.tofly.river.config.MinIoConfig;
|
|
|
+import com.tofly.river.exception.BusinessException;
|
|
|
+import io.minio.MinioClient;
|
|
|
+import io.minio.ObjectStat;
|
|
|
+import io.minio.PutObjectOptions;
|
|
|
+import io.minio.messages.Bucket;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import net.coobird.thumbnailator.Thumbnails;
|
|
|
+import org.apache.commons.compress.utils.IOUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipOutputStream;
|
|
|
+
|
|
|
+
|
|
|
+ * Java Client API参考文档:https:
|
|
|
+ * MinIo工具类
|
|
|
+ *
|
|
|
+ * @author BZ-DJJ
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class MinIoUtil {
|
|
|
+ @Autowired
|
|
|
+ MinIoConfig minicofig;
|
|
|
+
|
|
|
+ private static MinioClient minioClient;
|
|
|
+
|
|
|
+
|
|
|
+ * 初始化minio配置
|
|
|
+ *
|
|
|
+ * @param :
|
|
|
+ * @return: void
|
|
|
+ * @date : 2020/8/16 20:56
|
|
|
+ */
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ try {
|
|
|
+ minioClient = new MinioClient(minicofig.getUrl(), minicofig.getAccessKey(),
|
|
|
+ minicofig.getSecretKey());
|
|
|
+ createBucket(minicofig.getBucketName());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("初始化minio配置异常: 【{}】", e.fillInStackTrace());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 判断 bucket是否存在
|
|
|
+ *
|
|
|
+ * @param bucketName: 桶名
|
|
|
+ * @return: boolean
|
|
|
+ * @date : 2020/8/16 20:53
|
|
|
+ */
|
|
|
+ @SneakyThrows(Exception.class)
|
|
|
+ public static boolean bucketExists(String bucketName) {
|
|
|
+ return minioClient.bucketExists(bucketName);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 创建 bucket
|
|
|
+ *
|
|
|
+ * @param bucketName: 桶名
|
|
|
+ * @return: void
|
|
|
+ * @date : 2020/8/16 20:53
|
|
|
+ */
|
|
|
+ @SneakyThrows(Exception.class)
|
|
|
+ public static void createBucket(String bucketName) {
|
|
|
+ boolean isExist = minioClient.bucketExists(bucketName);
|
|
|
+ if (!isExist) {
|
|
|
+ minioClient.makeBucket(bucketName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取全部bucket
|
|
|
+ *
|
|
|
+ * @param :
|
|
|
+ * @return: java.util.List<io.minio.messages.Bucket>
|
|
|
+ * @date : 2020/8/16 23:28
|
|
|
+ */
|
|
|
+ @SneakyThrows(Exception.class)
|
|
|
+ public static List<Bucket> getAllBuckets() {
|
|
|
+ return minioClient.listBuckets();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 文件上传
|
|
|
+ *
|
|
|
+ * @param bucketName: 桶名
|
|
|
+ * @param fileName: 文件名
|
|
|
+ * @param filePath: 文件路径
|
|
|
+ * @return: void
|
|
|
+ * @date : 2020/8/16 20:53
|
|
|
+ */
|
|
|
+ @SneakyThrows(Exception.class)
|
|
|
+ public static void upload(String bucketName, String fileName, String filePath) {
|
|
|
+ minioClient.putObject(bucketName, fileName, filePath, null);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 文件上传
|
|
|
+ *
|
|
|
+ * @param bucketName: 桶名
|
|
|
+ * @param fileName: 文件名
|
|
|
+ * @param stream: 文件流
|
|
|
+ * @return: java.lang.String : 文件url地址
|
|
|
+ * @date : 2020/8/16 23:40
|
|
|
+ */
|
|
|
+ @SneakyThrows(Exception.class)
|
|
|
+ public static String upload(String bucketName, String fileName, InputStream stream) {
|
|
|
+ minioClient.putObject(bucketName, fileName, stream, new PutObjectOptions(stream.available(), -1));
|
|
|
+ return getFileUrl(bucketName, fileName);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 文件上传
|
|
|
+ *
|
|
|
+ * @param bucketName: 桶名
|
|
|
+ * @param file: 文件
|
|
|
+ * @param fileName 保存文件名
|
|
|
+ * @return: java.lang.String : 文件url地址
|
|
|
+ * @date : 2020/8/16 23:40
|
|
|
+ */
|
|
|
+ @SneakyThrows(Exception.class)
|
|
|
+ public static String upload(String bucketName, MultipartFile file,String fileName) {
|
|
|
+ final InputStream is = file.getInputStream();
|
|
|
+ minioClient.putObject(bucketName, fileName, is, new PutObjectOptions(is.available(), -1));
|
|
|
+ is.close();
|
|
|
+ return getFileUrl(bucketName, fileName);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 生成缩略图并上传
|
|
|
+ * @param image
|
|
|
+ * @param bucketName
|
|
|
+ * @param fileName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @SneakyThrows(Exception.class)
|
|
|
+ public static String upLoadThumbnail(BufferedImage image, String bucketName, String fileName) {
|
|
|
+ if (image != null) {
|
|
|
+ double v = (double) 64 / (double) image.getWidth();
|
|
|
+ try {
|
|
|
+ BufferedImage thumbnail = Thumbnails.of(image)
|
|
|
+ .scale(v)
|
|
|
+ .asBufferedImage();
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
+ ImageIO.write(thumbnail, "png", os);
|
|
|
+ InputStream input = new ByteArrayInputStream(os.toByteArray());
|
|
|
+ minioClient.putObject(bucketName, fileName, input, new PutObjectOptions(input.available(), -1));
|
|
|
+ input.close();
|
|
|
+ return getFileUrl(bucketName,fileName);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("生成缩略图失败:{}", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 判断文件为图片
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static BufferedImage getImage(MultipartFile file) {
|
|
|
+ BufferedImage bufferedImage = null;
|
|
|
+ try {
|
|
|
+ bufferedImage = ImageIO.read(file.getInputStream());
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return bufferedImage;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 删除文件
|
|
|
+ *
|
|
|
+ * @param bucketName: 桶名
|
|
|
+ * @param fileName: 文件名
|
|
|
+ * @return: void
|
|
|
+ * @date : 2020/8/16 20:53
|
|
|
+ */
|
|
|
+ @SneakyThrows(Exception.class)
|
|
|
+ public static void deleteFile(String bucketName, String fileName) {
|
|
|
+ minioClient.removeObject(bucketName, fileName);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 下载文件
|
|
|
+ *
|
|
|
+ * @param bucketName: 桶名
|
|
|
+ * @param fileName: 文件名
|
|
|
+ * @param response:
|
|
|
+ * @return: void
|
|
|
+ * @date : 2020/8/17 0:34
|
|
|
+ */
|
|
|
+ @SneakyThrows(Exception.class)
|
|
|
+ public static void download(String bucketName, String fileName, HttpServletResponse response) {
|
|
|
+
|
|
|
+ final ObjectStat stat = minioClient.statObject(bucketName, fileName);
|
|
|
+ response.setContentType(stat.contentType());
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
|
+ InputStream is = minioClient.getObject(bucketName, fileName);
|
|
|
+ IOUtils.copy(is, response.getOutputStream());
|
|
|
+ is.close();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 批量下载,并打包
|
|
|
+ * @param filePathNameList
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ */
|
|
|
+ public static void downloadFileList(Map<String, String> filePathNameList, HttpServletRequest request, HttpServletResponse response){
|
|
|
+
|
|
|
+ response.reset();
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ response.setContentType("multipart/form-data");
|
|
|
+
|
|
|
+ String dates = LocalDateTime.now()+"";
|
|
|
+
|
|
|
+ String billname = Constants.FJB+dates;
|
|
|
+ String downloadName = billname+Constants.POINT+Constants.ZIP;
|
|
|
+
|
|
|
+ String agent = request.getHeader("USER-AGENT");
|
|
|
+ try {
|
|
|
+
|
|
|
+ if (agent.contains("MSIE")||agent.contains("Trident")) {
|
|
|
+ downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8");
|
|
|
+ } else {
|
|
|
+
|
|
|
+ downloadName = new String(downloadName.getBytes("UTF-8"),"ISO-8859-1");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ response.setHeader("Content-Disposition", "attachment;fileName=\"" + downloadName + "\"");
|
|
|
+
|
|
|
+
|
|
|
+ ZipOutputStream zipos = null;
|
|
|
+ try {
|
|
|
+ zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
|
|
|
+
|
|
|
+ zipos.setMethod(ZipOutputStream.DEFLATED);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ AtomicReference<DataOutputStream> os = null;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ ZipOutputStream finalZipos = zipos;
|
|
|
+ filePathNameList.forEach((filePath, fileName)->{
|
|
|
+ File file = new File(filePath);
|
|
|
+ if (!file.exists()) {
|
|
|
+ throw new BusinessException("文件已不存在");
|
|
|
+ }else{
|
|
|
+ try {
|
|
|
+
|
|
|
+ finalZipos.putNextEntry(new ZipEntry(fileName));
|
|
|
+ os.set(new DataOutputStream(finalZipos));
|
|
|
+ InputStream is = new FileInputStream(file);
|
|
|
+ byte[] b = new byte[100];
|
|
|
+ int length = 0;
|
|
|
+ while((length = is.read(b))!= -1){
|
|
|
+ os.get().write(b, 0, length);
|
|
|
+ }
|
|
|
+ is.close();
|
|
|
+ finalZipos.closeEntry();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+ os.get().flush();
|
|
|
+ os.get().close();
|
|
|
+ zipos.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取minio文件的下载地址
|
|
|
+ *
|
|
|
+ * @param bucketName: 桶名
|
|
|
+ * @param fileName: 文件名
|
|
|
+ * @return: java.lang.String
|
|
|
+ * @date : 2020/8/16 22:07
|
|
|
+ */
|
|
|
+ @SneakyThrows(Exception.class)
|
|
|
+ public static String getFileUrl(String bucketName, String fileName) {
|
|
|
+ return minioClient.presignedGetObject(bucketName, fileName);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|