| | |
| | | */ |
| | | 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; |
| | |
| | | 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; |
| | | |
| | | /** |
| | | * 对象存储端点 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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://dev.jxpskj.com:9000"; |
| | | String access = "pskj"; |
| | | String secret = "pskj@2021"; |
| | | String bucket = "zhnc"; |
| | | 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://dev.jxpskj.com:9000"; |
| | | String access = "pskj"; |
| | | String secret = "pskj@2021"; |
| | | String bucket = "zhnc"; |
| | | 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 = TraceabilityCodeConfig.getUploadPath() + newName; |
| | | //数据封装 |
| | | Map<String, Object> map = new HashMap<>(2); |
| | | map.put("name", newName); |
| | | map.put("url", urls); |
| | | //返回 |
| | | return R.data(map); |
| | | } |
| | | |
| | | /** |
| | | * 上传文件 |
| | | * |
| | | * @param fileName 存储桶对象名称 |