智慧农业后台管理
guoshilong
2022-09-05 4e8eedbcaa35a3cce9e50dad54db9633e71d169f
src/main/java/org/springblade/modules/resource/endpoint/OssEndpoint.java
@@ -16,9 +16,12 @@
 */
package org.springblade.modules.resource.endpoint;
import io.minio.*;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springblade.common.config.TraceabilityCodeConfig;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.oss.model.OssFile;
@@ -31,8 +34,19 @@
import org.springblade.modules.resource.builder.oss.OssBuilder;
import org.springblade.modules.resource.entity.Attach;
import org.springblade.modules.resource.service.IAttachService;
import org.springblade.modules.system.entity.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
 * 对象存储端点
@@ -150,6 +164,110 @@
   }
   /**
    * base64 图片上传
    * @param base64 字符串
    * @return
    * @throws Exception
    */
   public String uploadBase64String(String base64) throws Exception {
      String[] split = base64.split(",");
      //图片上传
      BASE64Decoder decoder = new BASE64Decoder();
      // 解密
      byte[] b = decoder.decodeBuffer(split[1]);
      // 处理数据
//      for (int i = 0; i < b.length; ++i) {
//         if (b[i] < 0) {
//            b[i] += 256;
//         }
//      }
      //填写你文件上传的地址以及相应信息
      String url = "http://61.131.136.25:2081";
      String access = "zhbaadmin";
      String secret = "zhbapassword";
      String bucket = "zhba";
      MinioClient minioClient =
         MinioClient.builder()
            .endpoint(url)
            .credentials(access, secret)
            .build();
      // 检查存储桶是否已经存在
      boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
      if (!isExist) {
         // 创建一个名为zip的存储桶,用于zip文件。
         minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
         minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucket).build());
      }
      String newName = "upload/picture/" + UUID.randomUUID().toString().replaceAll("-", "") + ".jpg";
      InputStream in = new ByteArrayInputStream(b);
      //创建头部信息
      Map<String, String> headers = new HashMap<>(1 << 2);
      //添加自定义内容类型
      headers.put("Content-Type", "application/octet-stream");
      //上传
      minioClient.putObject(
         PutObjectArgs.builder().bucket(bucket).object(newName).stream(
            in, in.available(), -1)
            .headers(headers)
            .build());
      in.close();
      //url
      String urls = TraceabilityCodeConfig.getUploadPath() + newName;
      //返回
      return urls;
   }
   /**
    * 文件上传,自定义上传
    *
    * @param file 图片对象
    */
   @PostMapping("put-files")
   public R putFiles(@RequestParam MultipartFile file) throws IOException, ServerException, InsufficientDataException, InternalException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, XmlParserException, ErrorResponseException {
      //填写你文件上传的地址以及相应信息
      String url = "http://61.131.136.25:2081";
      String access = "zhbaadmin";
      String secret = "zhbapassword";
      String bucket = "zhba";
      MinioClient minioClient =
         MinioClient.builder()
            .endpoint(url)
            .credentials(access, secret)
            .build();
      // 检查存储桶是否已经存在
      boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
      if (!isExist) {
         // 创建一个名为zip的存储桶,用于zip文件。
         minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
         minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucket).build());
      }
      String fileName = file.getOriginalFilename();
      String newName = "upload/picture/" + UUID.randomUUID().toString().replaceAll("-", "")
         + fileName.substring(fileName.lastIndexOf("."));
      InputStream in = file.getInputStream();
      String[] split = newName.split("/");
      //创建头部信息
      Map<String, String> headers = new HashMap<>(1 << 2);
      //添加自定义内容类型
      headers.put("Content-Type", "application/octet-stream");
      //上传
      minioClient.putObject(
         PutObjectArgs.builder().bucket(bucket).object(newName).stream(
            in, in.available(), -1)
            .headers(headers)
            .build());
      in.close();
      String urls = "http://61.131.136.25:2081/zhba/" + newName;
      //数据封装
      Map<String, Object> map = new HashMap<>(2);
      map.put("name", newName);
      map.put("url", urls);
      //返回
      return R.data(map);
   }
   /**
    * 上传文件
    *
    * @param fileName 存储桶对象名称