From 97a058aa641a7e95c08778c8fb9ccb0f00cc5c56 Mon Sep 17 00:00:00 2001 From: wuwenbi Date: Fri, 3 Feb 2023 14:44:38 +0800 Subject: [PATCH 1/2] =?UTF-8?q?minio=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 11 + .../com/wwb/zhunong/config/MinioConfig.java | 25 ++ .../wwb/zhunong/config/WebAppConfigurer.java | 2 +- .../controller/UploadPicTestController.java | 45 +++ .../admin/AdminBigTypeController.java | 30 +- .../admin/AdminProductController.java | 70 +++- .../AdminProductSwiperImageController.java | 27 +- .../admin/AdminSmallTypeController.java | 2 +- .../java/com/wwb/zhunong/entity/BigType.java | 2 +- .../com/wwb/zhunong/entity/MinioProp.java | 19 + .../com/wwb/zhunong/entity/SmallType.java | 2 +- .../com/wwb/zhunong/entity/WxUserInfo.java | 2 +- .../java/com/wwb/zhunong/util/DateUtil.java | 6 + .../java/com/wwb/zhunong/util/MinIOUtil.java | 381 ++++++++++++++++++ src/main/resources/application.yml | 16 +- .../mybatis/mapper/BigTypeMapper.xml | 2 +- .../mybatis/mapper/SmallTypeMapper.xml | 10 +- 17 files changed, 602 insertions(+), 50 deletions(-) create mode 100644 src/main/java/com/wwb/zhunong/config/MinioConfig.java create mode 100644 src/main/java/com/wwb/zhunong/controller/UploadPicTestController.java create mode 100644 src/main/java/com/wwb/zhunong/entity/MinioProp.java create mode 100644 src/main/java/com/wwb/zhunong/util/MinIOUtil.java diff --git a/pom.xml b/pom.xml index 355e42c..2398339 100644 --- a/pom.xml +++ b/pom.xml @@ -119,6 +119,17 @@ commons-pool 1.6 + + + + io.minio + minio + 7.1.0 + + + org.springframework + spring-test + diff --git a/src/main/java/com/wwb/zhunong/config/MinioConfig.java b/src/main/java/com/wwb/zhunong/config/MinioConfig.java new file mode 100644 index 0000000..6e81d10 --- /dev/null +++ b/src/main/java/com/wwb/zhunong/config/MinioConfig.java @@ -0,0 +1,25 @@ +package com.wwb.zhunong.config; + +import com.wwb.zhunong.entity.MinioProp; +import io.minio.MinioClient; +import io.minio.errors.InvalidEndpointException; +import org.springframework.beans.InvalidPropertyException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import io.minio.errors.InvalidPortException; + +@Configuration +@EnableConfigurationProperties(MinioProp.class) +public class MinioConfig { + + @Autowired + private MinioProp minioProp; + + @Bean + public MinioClient minioClient() { + + return MinioClient.builder().endpoint(minioProp.getEndPoint()).credentials(minioProp.getAccessKey(), minioProp.getSecretKey()).build(); + } +} diff --git a/src/main/java/com/wwb/zhunong/config/WebAppConfigurer.java b/src/main/java/com/wwb/zhunong/config/WebAppConfigurer.java index cc4112b..41564f6 100644 --- a/src/main/java/com/wwb/zhunong/config/WebAppConfigurer.java +++ b/src/main/java/com/wwb/zhunong/config/WebAppConfigurer.java @@ -29,7 +29,7 @@ public class WebAppConfigurer implements WebMvcConfigurer { public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/image/swiper/**").addResourceLocations("file:E:\\zhunong_v1\\swiperImgs\\"); registry.addResourceHandler("/image/bigType/**").addResourceLocations("file:E:\\zhunong_v1\\bigTypeImgs\\"); - registry.addResourceHandler("/image/product/**").addResourceLocations("file:E:\\zhunong_v1\\productImgs\\"); +// registry.addResourceHandler("/image/product/**").addResourceLocations("file:E:\\zhunong_v1\\productImgs\\"); registry.addResourceHandler("/image/productSwiperImgs/**").addResourceLocations("file:E:\\zhunong_v1\\productSwiperImgs\\"); registry.addResourceHandler("/image/productIntroImgs/**").addResourceLocations("file:E:\\zhunong_v1\\productIntroImgs\\"); registry.addResourceHandler("/image/productParaImgs/**").addResourceLocations("file:E:\\zhunong_v1\\productParaImgs\\"); diff --git a/src/main/java/com/wwb/zhunong/controller/UploadPicTestController.java b/src/main/java/com/wwb/zhunong/controller/UploadPicTestController.java new file mode 100644 index 0000000..4ed096c --- /dev/null +++ b/src/main/java/com/wwb/zhunong/controller/UploadPicTestController.java @@ -0,0 +1,45 @@ +package com.wwb.zhunong.controller; + +import afu.org.checkerframework.checker.oigj.qual.O; +import com.wwb.zhunong.entity.R; +import com.wwb.zhunong.util.DateUtil; +import com.wwb.zhunong.util.MinIOUtil; +import io.minio.ObjectWriteResponse; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +@RestController +@RequestMapping("/pic") +public class UploadPicTestController { + + + + @PostMapping("/uploadpic") + public R uploadPic(MultipartFile file) throws Exception { + String bucket="pic-bucket"; + String filename = file.getOriginalFilename(); + String[] exts = filename.split("\\."); + String ext = exts[exts.length-1]; + String caselsh = filename.substring(0, filename.lastIndexOf(".")); + String objName = DateUtil.getCurrentDateStr2()+"."+ext; + String contentType = file.getContentType(); + MinIOUtil minIOUtil = new MinIOUtil(); + minIOUtil.initMinioClient(); + ObjectWriteResponse file1 = MinIOUtil.uploadFile(bucket,file,"product/"+objName,contentType); + String imgUrl = MinIOUtil.getBasisUrl() + "product/" + filename + "?t=" + System.currentTimeMillis(); + System.out.println("url="+imgUrl); +// Map map = new HashMap<>(); +// map.put("file1",file1); + return R.ok(); + } + +} diff --git a/src/main/java/com/wwb/zhunong/controller/admin/AdminBigTypeController.java b/src/main/java/com/wwb/zhunong/controller/admin/AdminBigTypeController.java index e3886ea..bdc9fdf 100644 --- a/src/main/java/com/wwb/zhunong/controller/admin/AdminBigTypeController.java +++ b/src/main/java/com/wwb/zhunong/controller/admin/AdminBigTypeController.java @@ -9,6 +9,7 @@ import com.wwb.zhunong.entity.SmallType; import com.wwb.zhunong.service.IBigTypeService; import com.wwb.zhunong.service.ISmallTypeService; import com.wwb.zhunong.util.DateUtil; +import com.wwb.zhunong.util.MinIOUtil; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -108,18 +109,29 @@ public class AdminBigTypeController { public Map uploadImage(MultipartFile file)throws Exception{ Map map=new HashMap(); if(!file.isEmpty()){ - // 获取文件名 - String fileName = file.getOriginalFilename(); - // 获取文件的后缀名 - String suffixName = fileName.substring(fileName.lastIndexOf(".")); - String newFileName= DateUtil.getCurrentDateStr()+suffixName; - //对图片进行复制 - FileUtils.copyInputStreamToFile(file.getInputStream(), new File(bigTypeImagesFilePath+newFileName)); +// // 获取文件名 +// String fileName = file.getOriginalFilename(); +// // 获取文件的后缀名 +// String suffixName = fileName.substring(fileName.lastIndexOf(".")); +// String newFileName= DateUtil.getCurrentDateStr()+suffixName; +// //对图片进行复制 +// FileUtils.copyInputStreamToFile(file.getInputStream(), new File(bigTypeImagesFilePath+newFileName)); + + String bucket="pic-bucket"; + String filename = file.getOriginalFilename(); + String[] exts = filename.split("\\."); + String ext = exts[exts.length-1]; + String objName = DateUtil.getCurrentDateStr2()+"."+ext; + String contentType = file.getContentType(); + MinIOUtil minIOUtil = new MinIOUtil(); + minIOUtil.initMinioClient(); + MinIOUtil.uploadFile(bucket,file,"bigTypeImg/"+objName,contentType); + String imgUrl = MinIOUtil.getBasisUrl() + "bigTypeImg/" + objName + "?t=" + System.currentTimeMillis(); map.put("code", 0); map.put("msg", "上传成功"); Map map2=new HashMap(); - map2.put("title", newFileName); - map2.put("src", "/image/bigType/"+newFileName); + map2.put("title", objName); + map2.put("src", imgUrl); map.put("data", map2); /* diff --git a/src/main/java/com/wwb/zhunong/controller/admin/AdminProductController.java b/src/main/java/com/wwb/zhunong/controller/admin/AdminProductController.java index fbaa57e..76d72c7 100644 --- a/src/main/java/com/wwb/zhunong/controller/admin/AdminProductController.java +++ b/src/main/java/com/wwb/zhunong/controller/admin/AdminProductController.java @@ -7,6 +7,8 @@ import com.wwb.zhunong.entity.Product; import com.wwb.zhunong.entity.R; import com.wwb.zhunong.service.IProductService; import com.wwb.zhunong.util.DateUtil; +import com.wwb.zhunong.util.MinIOUtil; +import io.minio.ObjectWriteResponse; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -139,19 +141,37 @@ public class AdminProductController { public Map uploadImage(MultipartFile file)throws Exception{ Map map=new HashMap(); if(!file.isEmpty()){ - // 获取文件名 - String fileName = file.getOriginalFilename(); - // 获取文件的后缀名 - String suffixName = fileName.substring(fileName.lastIndexOf(".")); - String newFileName= DateUtil.getCurrentDateStr()+suffixName; - - FileUtils.copyInputStreamToFile(file.getInputStream(), new File(productImagesFilePath+newFileName)); - map.put("code", 0); - map.put("msg", "上传成功"); +// // 获取文件名 +// String fileName = file.getOriginalFilename(); +// // 获取文件的后缀名 +// String suffixName = fileName.substring(fileName.lastIndexOf(".")); +// String newFileName= DateUtil.getCurrentDateStr()+suffixName; +// FileUtils.copyInputStreamToFile(file.getInputStream(), new File(productImagesFilePath+newFileName)); +// map.put("code", 0); +// map.put("msg", "上传成功"); +// Map map2=new HashMap(); +// map2.put("title", newFileName); +// map2.put("src", "/image/product/"+newFileName); +// map.put("data", map2); + String bucket="pic-bucket"; + String filename = file.getOriginalFilename(); + String[] exts = filename.split("\\."); + String ext = exts[exts.length-1]; + String objName = DateUtil.getCurrentDateStr2()+"."+ext; + String contentType = file.getContentType(); + MinIOUtil minIOUtil = new MinIOUtil(); + minIOUtil.initMinioClient(); + ObjectWriteResponse file1 = MinIOUtil.uploadFile(bucket,file,"product/"+objName,contentType); +// FileUtils.copyInputStreamToFile(file.getInputStream(), new File(productImagesFilePath+objName)); + String imgUrl = MinIOUtil.getBasisUrl() + "product/" + objName + "?t=" + System.currentTimeMillis(); + System.out.println("imgUrl="+imgUrl); Map map2=new HashMap(); - map2.put("title", newFileName); - map2.put("src", "/image/product/"+newFileName); + map2.put("title",objName); + map2.put("src", imgUrl); + map.put("code",0); + map.put("msg","上传成功"); map.put("data", map2); + } return map; } @@ -167,18 +187,28 @@ public class AdminProductController { public Map uploadSwiperImage(MultipartFile file)throws Exception{ Map map=new HashMap(); if(!file.isEmpty()){ - // 获取文件名 - String fileName = file.getOriginalFilename(); - // 获取文件的后缀名 - String suffixName = fileName.substring(fileName.lastIndexOf(".")); - String newFileName=DateUtil.getCurrentDateStr()+suffixName; - - FileUtils.copyInputStreamToFile(file.getInputStream(), new File(swiperImagesFilePath+newFileName)); +// // 获取文件名 +// String fileName = file.getOriginalFilename(); +// // 获取文件的后缀名 +// String suffixName = fileName.substring(fileName.lastIndexOf(".")); +// String newFileName=DateUtil.getCurrentDateStr()+suffixName; +// FileUtils.copyInputStreamToFile(file.getInputStream(), new File(swiperImagesFilePath+newFileName)); + + String bucket="pic-bucket"; + String filename = file.getOriginalFilename(); + String[] exts = filename.split("\\."); + String ext = exts[exts.length-1]; + String objName = DateUtil.getCurrentDateStr2()+"."+ext; + String contentType = file.getContentType(); + MinIOUtil minIOUtil = new MinIOUtil(); + minIOUtil.initMinioClient(); + MinIOUtil.uploadFile(bucket,file,"swiper/"+objName,contentType); + String imgUrl = MinIOUtil.getBasisUrl() + "swiper/" + objName + "?t=" + System.currentTimeMillis(); map.put("code", 0); map.put("msg", "上传成功"); Map map2=new HashMap(); - map2.put("title", newFileName); - map2.put("src", "/image/swiper/"+newFileName); + map2.put("title", objName); + map2.put("src", imgUrl); map.put("data", map2); } return map; diff --git a/src/main/java/com/wwb/zhunong/controller/admin/AdminProductSwiperImageController.java b/src/main/java/com/wwb/zhunong/controller/admin/AdminProductSwiperImageController.java index 96f0ecc..43e9323 100644 --- a/src/main/java/com/wwb/zhunong/controller/admin/AdminProductSwiperImageController.java +++ b/src/main/java/com/wwb/zhunong/controller/admin/AdminProductSwiperImageController.java @@ -6,6 +6,7 @@ import com.wwb.zhunong.entity.ProductSwiperImage; import com.wwb.zhunong.entity.R; import com.wwb.zhunong.service.IProductSwiperImageService; import com.wwb.zhunong.util.DateUtil; +import com.wwb.zhunong.util.MinIOUtil; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -52,18 +53,28 @@ public class AdminProductSwiperImageController { public Map uploadImage(MultipartFile file)throws Exception{ Map map=new HashMap(); if(!file.isEmpty()){ - // 获取文件名 - String fileName = file.getOriginalFilename(); - // 获取文件的后缀名 - String suffixName = fileName.substring(fileName.lastIndexOf(".")); - String newFileName= DateUtil.getCurrentDateStr()+suffixName; +// // 获取文件名 +// String fileName = file.getOriginalFilename(); +// // 获取文件的后缀名 +// String suffixName = fileName.substring(fileName.lastIndexOf(".")); +// String newFileName= DateUtil.getCurrentDateStr()+suffixName; +// FileUtils.copyInputStreamToFile(file.getInputStream(), new File(productSwiperImagesFilePath+newFileName)); - FileUtils.copyInputStreamToFile(file.getInputStream(), new File(productSwiperImagesFilePath+newFileName)); + String bucket="pic-bucket"; + String filename = file.getOriginalFilename(); + String[] exts = filename.split("\\."); + String ext = exts[exts.length-1]; + String objName = DateUtil.getCurrentDateStr2()+"."+ext; + String contentType = file.getContentType(); + MinIOUtil minIOUtil = new MinIOUtil(); + minIOUtil.initMinioClient(); + MinIOUtil.uploadFile(bucket,file,"proSwiper/"+objName,contentType); + String imgUrl = MinIOUtil.getBasisUrl() + "proSwiper/" + objName + "?t=" + System.currentTimeMillis(); map.put("code", 0); map.put("msg", "上传成功"); Map map2=new HashMap(); - map2.put("title", newFileName); - map2.put("src", "/image/productSwiperImgs/"+newFileName); + map2.put("title", objName); + map2.put("src", imgUrl); map.put("data", map2); } return map; diff --git a/src/main/java/com/wwb/zhunong/controller/admin/AdminSmallTypeController.java b/src/main/java/com/wwb/zhunong/controller/admin/AdminSmallTypeController.java index 42b9a8a..c621dec 100644 --- a/src/main/java/com/wwb/zhunong/controller/admin/AdminSmallTypeController.java +++ b/src/main/java/com/wwb/zhunong/controller/admin/AdminSmallTypeController.java @@ -32,7 +32,7 @@ public class AdminSmallTypeController { * @param pageBean * @return */ - @RequestMapping("/list") + @PostMapping("/list") public R list(@RequestBody PageBean pageBean){ System.out.println(pageBean); Map map=new HashMap<>(); diff --git a/src/main/java/com/wwb/zhunong/entity/BigType.java b/src/main/java/com/wwb/zhunong/entity/BigType.java index efbdea6..bbd330e 100644 --- a/src/main/java/com/wwb/zhunong/entity/BigType.java +++ b/src/main/java/com/wwb/zhunong/entity/BigType.java @@ -11,7 +11,7 @@ import java.util.List; * 商品大类 * */ -@TableName("t_bigType") +@TableName("t_bigtype") @Data public class BigType implements Serializable { diff --git a/src/main/java/com/wwb/zhunong/entity/MinioProp.java b/src/main/java/com/wwb/zhunong/entity/MinioProp.java new file mode 100644 index 0000000..350cb35 --- /dev/null +++ b/src/main/java/com/wwb/zhunong/entity/MinioProp.java @@ -0,0 +1,19 @@ +package com.wwb.zhunong.entity; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Data +@Component +@ConfigurationProperties(prefix = "minio") +public class MinioProp { + + private String endPoint;// 连接url + + private String accessKey;// 用户名 + + private String secretKey;// 密码 + + private String bucketName;// 桶名称 +} diff --git a/src/main/java/com/wwb/zhunong/entity/SmallType.java b/src/main/java/com/wwb/zhunong/entity/SmallType.java index aa96d6b..412d046 100644 --- a/src/main/java/com/wwb/zhunong/entity/SmallType.java +++ b/src/main/java/com/wwb/zhunong/entity/SmallType.java @@ -13,7 +13,7 @@ import java.util.List; * 商品小类 * */ -@TableName("t_smallType") +@TableName("t_smalltype") @Data public class SmallType implements Serializable { diff --git a/src/main/java/com/wwb/zhunong/entity/WxUserInfo.java b/src/main/java/com/wwb/zhunong/entity/WxUserInfo.java index 1b7c4ca..f511a0c 100644 --- a/src/main/java/com/wwb/zhunong/entity/WxUserInfo.java +++ b/src/main/java/com/wwb/zhunong/entity/WxUserInfo.java @@ -14,7 +14,7 @@ import java.util.Date; * 微信用户信息实体 * */ -@TableName("t_wxUserInfo") +@TableName("t_wxuserinfo") @Data public class WxUserInfo implements Serializable { diff --git a/src/main/java/com/wwb/zhunong/util/DateUtil.java b/src/main/java/com/wwb/zhunong/util/DateUtil.java index ebfd18e..58246e7 100644 --- a/src/main/java/com/wwb/zhunong/util/DateUtil.java +++ b/src/main/java/com/wwb/zhunong/util/DateUtil.java @@ -44,6 +44,12 @@ public class DateUtil { SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmssSSSSSSSSS"); return sdf.format(date); } + + public static String getCurrentDateStr2(){ + Date date=new Date(); + SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss"); + return sdf.format(date); + } public static String getCurrentDatePath()throws Exception{ Date date=new Date(); diff --git a/src/main/java/com/wwb/zhunong/util/MinIOUtil.java b/src/main/java/com/wwb/zhunong/util/MinIOUtil.java new file mode 100644 index 0000000..936726f --- /dev/null +++ b/src/main/java/com/wwb/zhunong/util/MinIOUtil.java @@ -0,0 +1,381 @@ +package com.wwb.zhunong.util; + +import io.minio.*; +import org.springframework.web.multipart.MultipartFile; +import io.minio.http.Method; +import io.minio.messages.Bucket; +import io.minio.messages.Item; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +/** + * MinIO工具类 + */ +@Component +@Slf4j +public class MinIOUtil { + /** + * MinioClient实例 + */ + private static MinioClient minioClient; + + + /** + * Bucket所在地域对应的Endpoint + */ + private static String endPoint="http://192.168.181.128:9000"; + + /** + * 存储桶名称 + */ + private static String bucketName="pic-bucket"; + + /** + * 用户名 + */ + private static String accessKey="NqhWmuYInBXNlVOM"; + + /** + * 密码 + */ + private static String secretKey="I60UzlnqEbkKAo9eyFYLY15SDj964ALM"; + + /** + * 分隔符 + */ + private static final String SEPARATOR = "/"; + + public MinIOUtil() {} + + public MinIOUtil(String endPoint, String bucketName, String accessKey, String secretKey) { + MinIOUtil.endPoint = endPoint; + MinIOUtil.bucketName = bucketName; + MinIOUtil.accessKey = accessKey; + MinIOUtil.secretKey = secretKey; + this.initMinioClient(); + System.out.println("endpoint="+endPoint+"bucketName="+bucketName+"accessKey="+accessKey+"secretKey="+secretKey); + } + + /** + * 创建基于Java端的MinioClient + */ + public void initMinioClient() { + try { + if (null == minioClient) { + log.info("开始创建MinioClient..."); + minioClient = MinioClient.builder().endpoint(endPoint).credentials(accessKey, secretKey).build(); + createBucket(bucketName); + log.info("MinioClient创建完毕..."); + } + } catch (Exception e) { + log.error("MinIO服务器异常:{0}", e); + } + } + + /** + * 获取上传文件前缀路径 + * + * @return 前缀路径 + */ + public static String getBasisUrl() { + return endPoint + SEPARATOR + bucketName + SEPARATOR; + } + + /****************************** Operate Bucket Start ******************************/ + + /** + * 启动SpringBoot容器的时候初始化Bucket,如果没有Bucket则创建 + */ + private static void createBucket(String bucketName) throws Exception { + if (!bucketExists(bucketName)) { + minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); + } + } + + /** + * 判断Bucket是否存在 + * + * @return true:存在,false:不存在 + */ + public static boolean bucketExists(String bucketName) throws Exception { + return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); + } + + /** + * 获得Bucket策略 + * + * @param bucketName 存储桶名称 + * @return Bucket策略 + */ + public static String getBucketPolicy(String bucketName) throws Exception { + return minioClient.getBucketPolicy(GetBucketPolicyArgs.builder().bucket(bucketName).build()); + } + + /** + * 获得所有Bucket列表 + * + * @return Bucket列表 + */ + public static List getAllBuckets() throws Exception { + return minioClient.listBuckets(); + } + + /** + * 根据存储桶名称获取其相关信息 + * + * @param bucketName 存储桶名称 + * @return 相关信息 + */ + public static Optional getBucket(String bucketName) throws Exception { + return getAllBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst(); + } + + /** + * 根据存储桶名称删除Bucket,true:删除成功;false:删除失败,文件或已不存在 + * + * @param bucketName 存储桶名称 + */ + public static void removeBucket(String bucketName) throws Exception { + minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build()); + } + /****************************** Operate Bucket End ******************************/ + + + /****************************** Operate Files Start ******************************/ + /** + * 判断文件是否存在 + * + * @param bucketName 存储桶名称 + * @param objectName 文件名 + * @return true:存在;false:不存在 + */ + public static boolean isObjectExist(String bucketName, String objectName) { + boolean exist = true; + try { + minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build()); + } catch (Exception e) { + exist = false; + } + return exist; + } + + /** + * 判断文件夹是否存在 + * + * @param bucketName 存储桶名称 + * @param objectName 文件夹名称 + * @return true:存在;false:不存在 + */ + public static boolean isFolderExist(String bucketName, String objectName) { + boolean exist = false; + try { + Iterable> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(objectName).recursive(false).build()); + for (Result result : results) { + Item item = result.get(); + if (item.isDir() && objectName.equals(item.objectName())) { + exist = true; + } + } + } catch (Exception e) { + exist = false; + } + return exist; + } + + /** + * 根据文件前缀查询文件 + * + * @param bucketName 存储桶名称 + * @param prefix 前缀 + * @param recursive 是否使用递归查询 + * @return MinioItem列表 + */ + public static List getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) throws Exception { + List list = new ArrayList<>(); + Iterable> objectsIterator = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(prefix).recursive(recursive).build()); + if (objectsIterator != null) { + for (Result o : objectsIterator) { + Item item = o.get(); + list.add(item); + } + } + return list; + } + + /** + * 获取文件流 + * + * @param bucketName 存储桶名称 + * @param objectName 文件名 + * @return 二进制流 + */ + public static InputStream getObject(String bucketName, String objectName) throws Exception { + return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build()); + } + + /** + * 断点下载 + * + * @param bucketName 存储桶名称 + * @param objectName 文件名称 + * @param offset 起始字节的位置 + * @param length 要读取的长度 + * @return 二进制流 + */ + public InputStream getObject(String bucketName, String objectName, long offset, long length) throws Exception { + return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).offset(offset).length(length).build()); + } + + /** + * 获取路径下文件列表 + * + * @param bucketName 存储桶名称 + * @param prefix 文件名称 + * @param recursive 是否递归查找,false:模拟文件夹结构查找 + * @return 二进制流 + */ + public static Iterable> listObjects(String bucketName, String prefix, boolean recursive) { + return minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(prefix).recursive(recursive).build()); + } + + /** + * 使用MultipartFile进行文件上传 + * + * @param bucketName 存储桶名称 + * @param file 文件名 + * @param objectName 对象名 + * @param contentType 类型 + * @return ObjectWriteResponse对象 + */ + public static ObjectWriteResponse uploadFile(String bucketName, MultipartFile file, String objectName, String contentType) throws Exception { + InputStream inputStream = file.getInputStream(); + return minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).contentType(contentType).stream(inputStream, inputStream.available(), -1).build()); + } + + /** + * 上传本地文件 + * + * @param bucketName 存储桶名称 + * @param objectName 对象名称 + * @param fileName 本地文件路径 + */ + public static ObjectWriteResponse uploadFile(String bucketName, String objectName, String fileName) throws Exception { + return minioClient.uploadObject(UploadObjectArgs.builder().bucket(bucketName).object(objectName).filename(fileName).build()); + } + + /** + * 通过流上传文件 + * + * @param bucketName 存储桶名称 + * @param objectName 文件对象 + * @param inputStream 文件流 + */ + public static ObjectWriteResponse uploadFile(String bucketName, String objectName, InputStream inputStream) throws Exception { + return minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(inputStream, inputStream.available(), -1).build()); + } + + /** + * 创建文件夹或目录 + * + * @param bucketName 存储桶名称 + * @param objectName 目录路径 + */ + public static ObjectWriteResponse createDir(String bucketName, String objectName) throws Exception { + return minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(new ByteArrayInputStream(new byte[]{}), 0, -1).build()); + } + + /** + * 获取文件信息, 如果抛出异常则说明文件不存在 + * + * @param bucketName 存储桶名称 + * @param objectName 文件名称 + */ + public static String getFileStatusInfo(String bucketName, String objectName) throws Exception { + return minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build()).toString(); + } + + /** + * 拷贝文件 + * + * @param bucketName 存储桶名称 + * @param objectName 文件名 + * @param srcBucketName 目标存储桶 + * @param srcObjectName 目标文件名 + */ + public static ObjectWriteResponse copyFile(String bucketName, String objectName, String srcBucketName, String srcObjectName) throws Exception { + return minioClient.copyObject(CopyObjectArgs.builder().source(CopySource.builder().bucket(bucketName).object(objectName).build()).bucket(srcBucketName).object(srcObjectName).build()); + } + + /** + * 删除文件 + * + * @param bucketName 存储桶名称 + * @param objectName 文件名称 + */ + public static void removeFile(String bucketName, String objectName) throws Exception { + minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build()); + } + + /** + * 批量删除文件 + * + * @param bucketName 存储桶名称 + * @param keys 需要删除的文件列表 + */ + public static void removeFiles(String bucketName, List keys) { + keys.forEach(key -> { + try { + removeFile(bucketName, key); + } catch (Exception e) { + log.error("批量删除失败!error:{0}", e); + } + }); + } + + /** + * 获取文件外链 + * + * @param bucketName 存储桶名称 + * @param objectName 文件名 + * @param expires 过期时间 <=7 秒 (外链有效时间(单位:秒)) + * @return 文件外链 + */ + public static String getPreSignedObjectUrl(String bucketName, String objectName, Integer expires) throws Exception { + GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().expiry(expires).bucket(bucketName).object(objectName).build(); + return minioClient.getPresignedObjectUrl(args); + } + + /** + * 获得文件外链 + * + * @param bucketName 存储桶名称 + * @param objectName 文件名 + * @return 文件外链 + */ + public static String getPreSignedObjectUrl(String bucketName, String objectName) throws Exception { + GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(objectName).method(Method.GET).build(); + return minioClient.getPresignedObjectUrl(args); + } + +// public static String getPreviewFileUrl(String fileName) { +// return minioClient.getPresignedObjectUrl(getPreviewFileUrl(fileName)); +// } + /** + * 将URLDecoder编码转成UTF8 + * + * @param str 字符串 + * @return 编码 + */ + public static String getUtf8ByDecoder(String str) throws UnsupportedEncodingException { + String url = str.replaceAll("%(?![0-9a-fA-F]{2})", "%25"); + return URLDecoder.decode(url, "UTF-8"); + } +} + diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index fefd2b7..94864a0 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -7,12 +7,16 @@ spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://localhost:3306/db_zhunong?serverTimezone=Asia/Shanghai + url: jdbc:mysql://192.168.181.128:3306/db_zhunong?serverTimezone=Asia/Shanghai&characterEncoding=utf8 username: root password: 123456 redis: host: 127.0.0.1 port: 6379 + servlet: + multipart: + max-file-size: 20MB + max-request-size: 20MB mybatis-plus: global-config: @@ -24,6 +28,14 @@ mybatis-plus: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl mapper-locations: classpath:mybatis/mapper/*.xml +# minio 文件存储配置信息 +minio: + endpoint: http://192.168.181.128:9001 + accesskey: NqhWmuYInBXNlVOM + secretkey: I60UzlnqEbkKAo9eyFYLY15SDj964ALM + bucket: pic-bucket + + weixin: jscode2sessionUrl: https://api.weixin.qq.com/sns/jscode2session appid: wxc3e1887cb270070b @@ -41,7 +53,7 @@ weixinpayconfig: bigTypeImagesFilePath: E://zhunong_v1/bigTypeImgs/ -productImagesFilePath: E://zhunong_v1/productImgs/ +productImagesFilePath: http://127.0.0.1:9000/pic-bucket/product/ swiperImagesFilePath: E://zhunong_v1/swiperImgs/ diff --git a/src/main/resources/mybatis/mapper/BigTypeMapper.xml b/src/main/resources/mybatis/mapper/BigTypeMapper.xml index 8f4c1ef..34e9d58 100644 --- a/src/main/resources/mybatis/mapper/BigTypeMapper.xml +++ b/src/main/resources/mybatis/mapper/BigTypeMapper.xml @@ -8,7 +8,7 @@ diff --git a/src/main/resources/mybatis/mapper/SmallTypeMapper.xml b/src/main/resources/mybatis/mapper/SmallTypeMapper.xml index 2b471c6..d63b549 100644 --- a/src/main/resources/mybatis/mapper/SmallTypeMapper.xml +++ b/src/main/resources/mybatis/mapper/SmallTypeMapper.xml @@ -9,11 +9,11 @@