| | |
| | | package com.panzhihua.applets.config; |
| | | |
| | | import com.panzhihua.common.utlis.DateUtils; |
| | | import io.minio.MinioClient; |
| | | import io.minio.ObjectStat; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.bouncycastle.util.encoders.Base64; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.*; |
| | | import java.net.URLEncoder; |
| | | import java.util.UUID; |
| | | |
| | |
| | | /** |
| | | * 文件上传 |
| | | * |
| | | * @param file |
| | | * 要上传的文件 |
| | | * @param file 要上传的文件 |
| | | * @return |
| | | */ |
| | | public String upload(MultipartFile file, String name) { |
| | |
| | | try { |
| | | UUID uuid = UUID.randomUUID(); |
| | | StringBuilder s = new StringBuilder(); |
| | | s.append(uuid.toString().replace("-", "")).append("/"); |
| | | s.append(DateUtils.getCurrentDateyymd()).append("-original").append("/"); |
| | | MinioClient minioClient = new MinioClient(minioProperties.getHost(), minioProperties.getAccessKey(), |
| | | minioProperties.getSecretKey()); |
| | | minioProperties.getSecretKey()); |
| | | // bucket 不存在,创建 |
| | | if (!minioClient.bucketExists(minioProperties.getBucket())) { |
| | | minioClient.makeBucket(minioProperties.getBucket()); |
| | |
| | | String fileName = s.append(name).toString(); |
| | | String contentType = file.getContentType(); |
| | | minioClient.putObject(minioProperties.getBucket(), fileName, input, contentType); |
| | | StringBuilder fileUrl = new StringBuilder(minioProperties.getUrl()); |
| | | String url = fileUrl.append(fileName).toString(); |
| | | return url; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 文件上传 |
| | | * |
| | | * @param input |
| | | * 要上传的文件 |
| | | * @return |
| | | */ |
| | | public String uploadInputStream(InputStream input, String name) { |
| | | if (null != input) { |
| | | try { |
| | | UUID uuid = UUID.randomUUID(); |
| | | StringBuilder s = new StringBuilder(); |
| | | s.append(DateUtils.getCurrentDateyymd()).append("/"); |
| | | MinioClient minioClient = new MinioClient(minioProperties.getHost(), minioProperties.getAccessKey(), |
| | | minioProperties.getSecretKey()); |
| | | if (!minioClient.bucketExists(minioProperties.getBucket())) { |
| | | minioClient.makeBucket(minioProperties.getBucket()); |
| | | } |
| | | String fileName = s.append(name).toString(); |
| | | minioClient.putObject(minioProperties.getBucket(), fileName, input, "application/octet-stream"); |
| | | StringBuilder fileUrl = new StringBuilder(minioProperties.getUrl()); |
| | | String url = fileUrl.append(fileName).toString(); |
| | | return url; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | /** |
| | | * 文件上传 |
| | | * |
| | | * @param file 要上传的文件 |
| | | * @return |
| | | */ |
| | | public String uploadRetFileName(MultipartFile file, String name) { |
| | | if (null != file) { |
| | | try { |
| | | UUID uuid = UUID.randomUUID(); |
| | | StringBuilder s = new StringBuilder(); |
| | | s.append(DateUtils.getCurrentDateyymd()).append("-original").append("/"); |
| | | MinioClient minioClient = new MinioClient(minioProperties.getHost(), minioProperties.getAccessKey(), |
| | | minioProperties.getSecretKey()); |
| | | // bucket 不存在,创建 |
| | | if (!minioClient.bucketExists(minioProperties.getBucket())) { |
| | | minioClient.makeBucket(minioProperties.getBucket()); |
| | | } |
| | | // 得到文件流 |
| | | InputStream input = file.getInputStream(); |
| | | // 文件名 |
| | | // String fileName = uuid + "/images." + FilenameUtils.getExtension(file.getOriginalFilename()); |
| | | String fileName = s.append(name).toString(); |
| | | String contentType = file.getContentType(); |
| | | minioClient.putObject(minioProperties.getBucket(), fileName, input, "application/octet-stream"); |
| | | StringBuilder fileUrl = new StringBuilder(minioProperties.getUrl()); |
| | | String url = fileUrl.append(fileName).toString(); |
| | | return fileName; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 文件上传 |
| | | * |
| | | * @param file 要上传的文件 |
| | | * @return |
| | | */ |
| | | public String uploadFile(File file, String name) { |
| | | if (null != file) { |
| | | try { |
| | | StringBuilder s = new StringBuilder(); |
| | | s.append(DateUtils.getCurrentDateyymd()).append("/"); |
| | | MinioClient minioClient = new MinioClient(minioProperties.getHost(), minioProperties.getAccessKey(), |
| | | minioProperties.getSecretKey()); |
| | | // bucket 不存在,创建 |
| | | if (!minioClient.bucketExists(minioProperties.getBucket())) { |
| | | minioClient.makeBucket(minioProperties.getBucket()); |
| | | } |
| | | // 得到文件流 |
| | | InputStream input = new FileInputStream(file); |
| | | // 文件名 |
| | | // String fileName = uuid + "/images." + FilenameUtils.getExtension(file.getOriginalFilename()); |
| | | String fileName = s.append(name).toString(); |
| | | minioClient.putObject(minioProperties.getBucket(), fileName, input, ""); |
| | | StringBuilder fileUrl = new StringBuilder(minioProperties.getUrl()); |
| | | String url = fileUrl.append(fileName).toString(); |
| | | return url; |
| | |
| | | InputStream inputStream; |
| | | try { |
| | | MinioClient minioClient = new MinioClient(minioProperties.getHost(), minioProperties.getAccessKey(), |
| | | minioProperties.getSecretKey()); |
| | | minioProperties.getSecretKey()); |
| | | ObjectStat stat = minioClient.statObject(minioProperties.getBucket(), fileName); |
| | | inputStream = minioClient.getObject(minioProperties.getBucket(), fileName); |
| | | response.setContentType(stat.contentType()); |
| | |
| | | byte[] bytes = new byte[0]; |
| | | try { |
| | | MinioClient minioClient = new MinioClient(minioProperties.getHost(), minioProperties.getAccessKey(), |
| | | minioProperties.getSecretKey()); |
| | | minioProperties.getSecretKey()); |
| | | inputStream = minioClient.getObject(minioProperties.getBucket(), fileName); |
| | | bytes = toByteArray(inputStream); |
| | | } catch (Exception e) { |
| | |
| | | } |
| | | return bytes; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 删除 |
| | | * |
| | | * @param fileName |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public String removeMinio(String fileName) { |
| | | try { |
| | | //创建MinioClient对象 |
| | | //fileName = "6edbdf8c2a1146ddaf4fbfcb30f804ad/72b2a44c18a54802854931a65b604576.jpg" |
| | | MinioClient minioClient = new MinioClient(minioProperties.getHost(), minioProperties.getAccessKey(), |
| | | minioProperties.getSecretKey()); |
| | | minioClient.removeObject(minioProperties.getBucket(), fileName); |
| | | return "success"; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return e.getMessage(); |
| | | } |
| | | |
| | | } |
| | | |
| | | public InputStream base64StrToInputStream(String base64string) { |
| | | ByteArrayInputStream stream = null; |
| | | try { |
| | | byte[] bytes = Base64.decode(base64string); |
| | | stream = new ByteArrayInputStream(bytes); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return stream; |
| | | } |
| | | } |