Administrator
2021-08-10 c8ebf3038d2a7f69d9eb40f5a276dc3fa826fa9b
src/main/java/org/springblade/modules/resource/endpoint/OssEndpoint.java
@@ -16,9 +16,21 @@
 */
package org.springblade.modules.resource.endpoint;
import com.alibaba.fastjson.JSON;
import io.minio.*;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springblade.common.utils.arg;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.oss.model.OssFile;
@@ -33,6 +45,14 @@
import org.springblade.modules.resource.service.IAttachService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
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;
/**
 * 对象存储端点
@@ -149,6 +169,50 @@
      return R.data(bladeFile);
   }
   /**
    *
    * app 文件上传
    * @param file 图片对象
    */
   @PostMapping("put-file-app")
   public R putFileApp(@RequestParam MultipartFile file) throws IOException, ServerException, InsufficientDataException, InternalException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, XmlParserException, ErrorResponseException {
      //填写你文件上传的地址以及相应信息
      String url = "http://192.168.0.109:9000";
      String access = "minioadmin";
      String secret = "minioadmin";
      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();
      //创建头部信息
      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://192.168.0.109:9000/zhba/" + newName;
      return R.data(urls);
   }
   /**
    * 上传文件
    *
@@ -203,7 +267,7 @@
    * @param bladeFile 对象存储文件
    * @return attachId
    */
   private Long buildAttach(String fileName, Long fileSize, BladeFile bladeFile,String deptid,String type) {
   private Long buildAttach(String fileName, Long fileSize, BladeFile bladeFile,String deptid,String type) throws Exception {
      String fileExtension = FileUtil.getFileExtension(fileName);
      Attach attach = new Attach();
      attach.setDomain(bladeFile.getDomain());
@@ -215,6 +279,7 @@
      attach.setDeptid(deptid);
      attach.setType(type);
      attachService.save(attach);
//      arg.test01(arg.url+"/blade-resource/attach/save",attach);
      return attach.getId();
   }