智慧保安后台管理-外网项目备份
tangzy
2021-08-05 60f3b50baef73b3c8d928c7059632f8df892fafc
src/main/java/org/springblade/modules/resource/endpoint/OssEndpoint.java
@@ -17,6 +17,8 @@
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;
@@ -28,6 +30,7 @@
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;
@@ -42,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;
/**
 * 对象存储端点
@@ -158,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);
   }
   /**
    * 上传文件
    *
@@ -224,23 +279,10 @@
      attach.setDeptid(deptid);
      attach.setType(type);
      attachService.save(attach);
      test01("http://192.168.0.104:80/blade-resource/attach/save",attach);
      arg.test01(arg.url+"/blade-resource/attach/save",attach);
      return attach.getId();
   }
   public static String test01(String path,Object obj) throws Exception{
      CloseableHttpClient httpClient = HttpClients.createDefault();
      HttpPost httpPost = new HttpPost(path);
      httpPost.setEntity(new StringEntity(JSON.toJSONString(obj),
         ContentType.create("application/json", "UTF-8")));
      CloseableHttpResponse execute = httpClient.execute(httpPost);
      HttpEntity entity = execute.getEntity();
      String str = EntityUtils.toString(entity);
      httpPost.clone();
      httpClient.close();
      System.out.println(str);
      return str;
   }
   /**
    * 删除文件
    *