From c6230804a1d6078ed565cb855a471502675b03cd Mon Sep 17 00:00:00 2001 From: songhuangfeng123 <shf18767906695@163.com> Date: 星期二, 09 八月 2022 18:31:39 +0800 Subject: [PATCH] 文件上传 --- /dev/null | 122 -------------------- safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioFile/utils/UUIDUtil.java | 25 ++++ safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioFile/controller/MinioController.java | 27 ++++ equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/model/dto/resp/KeypointEquipmentInfoDto.java | 10 + equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/service/impl/KeypointEquipmentInfoServiceImpl.java | 26 ++++ safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioFile/config/MinioConfig.java | 2 safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioFile/service/MinioAccessService.java | 97 ++++++++++++++++ 7 files changed, 186 insertions(+), 123 deletions(-) diff --git a/equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/model/dto/resp/KeypointEquipmentInfoDto.java b/equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/model/dto/resp/KeypointEquipmentInfoDto.java index 1520b20..541a34e 100644 --- a/equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/model/dto/resp/KeypointEquipmentInfoDto.java +++ b/equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/model/dto/resp/KeypointEquipmentInfoDto.java @@ -12,6 +12,8 @@ private Long id; //类型/类别外键 private Long equipmentTypeId; + //类型名称 + private String equipmentTypeName; //名称 private String name; //单位部门外键 @@ -356,4 +358,12 @@ public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } + + public String getEquipmentTypeName() { + return equipmentTypeName; + } + + public void setEquipmentTypeName(String equipmentTypeName) { + this.equipmentTypeName = equipmentTypeName; + } } \ No newline at end of file diff --git a/equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/service/impl/KeypointEquipmentInfoServiceImpl.java b/equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/service/impl/KeypointEquipmentInfoServiceImpl.java index b0d0434..1c87836 100644 --- a/equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/service/impl/KeypointEquipmentInfoServiceImpl.java +++ b/equipment/equipment-service/src/main/java/com/gkhy/safePlatform/equipment/service/impl/KeypointEquipmentInfoServiceImpl.java @@ -1,5 +1,6 @@ package com.gkhy.safePlatform.equipment.service.impl; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @@ -7,6 +8,7 @@ import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService; import com.gkhy.safePlatform.equipment.entity.*; import com.gkhy.safePlatform.equipment.model.dto.req.*; +import com.gkhy.safePlatform.equipment.repository.EquipmentTypeMngRepository; import com.gkhy.safePlatform.equipment.repository.KeypointEquipmentInfoRepository; import com.gkhy.safePlatform.equipment.service.*; import org.apache.dubbo.config.annotation.DubboReference; @@ -58,6 +60,9 @@ @Autowired private EquipmentInfoService equipmentInfoService; + + @Autowired + private EquipmentTypeMngRepository equipmentTypeMngRepository; @Override public ResultVO queryAll(PageQuery<KeypointEquipmentInfoQueryCriteria> pageQuery) { @@ -69,6 +74,7 @@ QueryHelpPlus.getPredicate(KeypointEquipmentInfo.class, pageQuery.getSearchParams())); List<KeypointEquipmentInfoDto> respList = BeanCopyUtils.copyBeanList(page.getRecords(), KeypointEquipmentInfoDto.class); + Set<Long> collectTypeIdSet = new HashSet(); //获取额外信息 respList.forEach(f->{ //设备保养 @@ -95,6 +101,8 @@ List<EquipmentRepairDetail> repairDetailList = equipmentRepairDetailService.queryAll(criteria2); f.setRepaireDetailList(repairDetailList); + // --------------------------- 获取设备设施类型管理ID----------------------- + collectTypeIdSet.add(f.getEquipmentTypeId()); // --------------------------- 获取部门信息----------------------- //收集所用到的部门ID @@ -113,6 +121,20 @@ checkDetailList.forEach(info->{info.setTestPersonDepartmentName(depNameMap.get(info.getTestPersonDepartmentId()));}); repairDetailList.forEach(info->{info.setRepairPersonDepartmentName(depNameMap.get(info.getRepairPersonDepartmentId()));}); }); + + + //获取设备设施类型名称 + if(!collectTypeIdSet.isEmpty()){ + List<EquipmentTypeMng> typeMngList = equipmentTypeMngRepository.selectList(new QueryWrapper<EquipmentTypeMng>().in("id",collectTypeIdSet)); + Map<Long, String> typePool = new HashMap<>(); + typeMngList.forEach(f->{ + typePool.put(f.getId(),f.getTypeName()); + }); + respList.forEach(f->{ + f.setEquipmentTypeName(typePool.get(f.getEquipmentTypeId())); + }); + } + return new SearchResultVO<>( true, pageIndex, @@ -193,6 +215,10 @@ checkDetailList.forEach(info->{info.setTestPersonDepartmentName(depNameMap.get(info.getTestPersonDepartmentId()));}); repairDetailList.forEach(info->{info.setRepairPersonDepartmentName(depNameMap.get(info.getRepairPersonDepartmentId()));}); + EquipmentTypeMng typeMng = equipmentTypeMngRepository.selectOne(new QueryWrapper<EquipmentTypeMng>().eq("id",infoDto.getEquipmentTypeId())); + if(typeMng != null){ + infoDto.setEquipmentTypeName(typeMng.getTypeName()); + } return infoDto; } diff --git a/incident-manage/incident-manage-service/src/main/resources/config/mapper/incidentManage/AccidentWorkApproveInfoMapper.xml b/incident-manage/incident-manage-service/src/main/resources/config/mapper/incidentManage/AccidentWorkApproveInfoMapper.xml deleted file mode 100644 index d168c06..0000000 --- a/incident-manage/incident-manage-service/src/main/resources/config/mapper/incidentManage/AccidentWorkApproveInfoMapper.xml +++ /dev/null @@ -1,111 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" ?> -<!DOCTYPE mapper - PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> -<mapper namespace="com.gkhy.safePlatform.incidentManage.repository.AccidentWorkApproveInfoRepository"> - - <resultMap type="com.gkhy.safePlatform.incidentManage.entity.AccidentWorkApproveInfoPageDO" id="accidentWorkApproveInfoPageDOResult"> - <id column="id" property="id" jdbcType="BIGINT"/> - <result column="gmt_create" property="gmtCreate"/> - <result column="work_name" property="workName"/> - <result column="title" property="title"/> - <result column="submit_person_id" property="submitPersonId"/> - <result column="approve_status" property="approveStatus"/> - </resultMap> - - - <select id="selectWorkApproveList" resultMap="accidentWorkApproveInfoPageDOResult"> - select id,`work_name`,`title`,`submit_person_id`,`approve_status`,gmt_create from accident_work_approve where del_flag = 0 - <if test="query.startTime != null "> and gmt_create <![CDATA[ >= ]]> #{query.startTime}</if> - <if test="query.endTime != null "> and gmt_create <![CDATA[ <= ]]> #{query.endTime}</if> - </select> - - <insert id="addWorkApprove" parameterType="com.gkhy.safePlatform.incidentManage.entity.AccidentWorkApproveInfo" - keyProperty="id" useGeneratedKeys="true"> - insert into accident_work_approve - <trim prefix="(" suffix=")" suffixOverrides=","> - <if test="id != null ">id,</if> - <if test="delFlag != null ">del_flag,</if> - <if test="gmtCreate != null ">gmt_create,</if> - <if test="gmtModitify != null ">gmt_moditify,</if> - <if test="createUid != null ">create_uid,</if> - <if test="updateUid != null ">update_uid,</if> - - <if test="workName != null and workName != ''">work_name,</if> - <if test="title != null and title != ''">title,</if> - <if test="submitPersonId != null ">submit_person_id,</if> - <if test="approvePersonId != null ">approve_person_id,</if> - <if test="approveStatus != null ">approve_status,</if> - <if test="approveResult != null ">approve_result,</if> - <if test="approveMemo != null and approveMemo != ''">approve_memo,</if> - <if test="relateType != null ">relate_type,</if> - <if test="relateId != null ">relate_id,</if> - <if test="relateDesc != null and relateDesc != ''">relate_desc,</if> - </trim> - <trim prefix="values (" suffix=")" suffixOverrides=","> - <if test="id != null ">#{id},</if> - <if test="delFlag != null ">#{delFlag},</if> - <if test="gmtCreate != null ">#{gmtCreate},</if> - <if test="gmtModitify != null ">#{gmtModitify},</if> - <if test="createUid != null ">#{createUid},</if> - <if test="updateUid != null ">#{updateUid},</if> - - <if test="workName != null and workName != ''">#{workName},</if> - <if test="title != null and title != ''">#{title},</if> - <if test="submitPersonId != null ">#{submitPersonId},</if> - <if test="approvePersonId != null ">#{approvePersonId},</if> - <if test="approveStatus != null ">#{approveStatus},</if> - <if test="approveResult != null ">#{approveResult},</if> - <if test="approveMemo != null and approveMemo != ''">#{approveMemo},</if> - <if test="relateType != null ">#{relateType},</if> - <if test="relateId != null ">#{relateId},</if> - <if test="relateDesc != null and relateDesc != ''">#{relateDesc},</if> - </trim> - </insert> - - <resultMap type="com.gkhy.safePlatform.incidentManage.entity.AccidentWorkApproveInfoDetailDO" - id="WorkApproveInfoDetailDOResult"> - <id column="id" property="id" jdbcType="BIGINT"/> - <result column="work_name" property="workName"/> - <result column="title" property="title"/> - <result column="submit_person_id" property="submitPersonId"/> - <result column="approve_person_id" property="approvePersonId"/> - <result column="approve_status" property="approveStatus"/> - <result column="approve_result" property="approveResult"/> - <result column="approve_memo" property="approveMemo"/> - <result column="relate_type" property="relateType"/> - <result column="relate_id" property="relateId"/> - <result column="relate_desc" property="relateDesc"/> - </resultMap> - - <select id="selectWorkApproveById" resultMap="WorkApproveInfoDetailDOResult"> - select id,gmt_create ,work_name,title,`submit_person_id`,`approve_person_id`,`approve_status`,approve_result,`approve_memo`, - `relate_type`,relate_id,`relate_desc` - from accident_work_approve where del_flag = 0 and id = #{id} - </select> - - <update id="updateWorkApprove" parameterType="com.gkhy.safePlatform.incidentManage.entity.AccidentWorkApproveInfo"> - update accident_work_approve - <trim prefix="SET" suffixOverrides=","> - <if test="gmtModitify != null ">gmt_moditify = #{gmtModitify},</if> - <if test="updateUid != null ">update_uid = #{updateUid},</if> - - <if test="workName != null and workName != ''">work_name = #{workName},</if> - <if test="title != null and title != ''">title = #{title},</if> - <if test="submitPersonId != null ">submit_person_id = #{submitPersonId},</if> - <if test="approvePersonId != null ">approve_person_id = #{approvePersonId},</if> - <if test="approveStatus != null ">approve_status = #{approveStatus},</if> - <if test="approveResult != null ">approve_result = #{approveResult},</if> - <if test="approveMemo != null and approveMemo != ''">approve_memo = #{approveMemo},</if> - <if test="relateType != null ">relate_type = #{relateType},</if> - <if test="relateId != null ">relate_id = #{relateId},</if> - <if test="relateDesc != null and relateDesc != ''">relate_desc = #{relateDesc},</if> - </trim> - where id = #{id} - </update> - - <update id="deleteWorkApproveById"> - update accident_work_approve set del_flag = 1 where id = #{id} - </update> - -</mapper> diff --git a/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioDemo/controller/FileController.java b/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioDemo/controller/FileController.java deleted file mode 100644 index 629a787..0000000 --- a/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioDemo/controller/FileController.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.gkhy.safePlatform.minioDemo.controller; - -import com.gkhy.safePlatform.commons.vo.ResultVO; -import com.gkhy.safePlatform.minioDemo.service.FileService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.multipart.MultipartFile; - -import javax.servlet.http.HttpServletResponse; - - -@RestController -@RequestMapping(value = "/file") -public class FileController { - - @Autowired - private FileService fileService; - - @RequestMapping(value = "/upload",method = RequestMethod.POST) - public ResultVO uploadFile(@RequestParam MultipartFile file , @RequestParam Integer moduleType){ - return fileService.uploadFile(file,moduleType); - } - - @RequestMapping(value = "/download",method = RequestMethod.GET) - public void downloadFile(String fileName,HttpServletResponse response){ - fileService.downloadFile(fileName,response); - } -} diff --git a/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioDemo/enums/ModuleTypeEnums.java b/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioDemo/enums/ModuleTypeEnums.java deleted file mode 100644 index d8207c1..0000000 --- a/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioDemo/enums/ModuleTypeEnums.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.gkhy.safePlatform.minioDemo.enums; - -public enum ModuleTypeEnums { - - EMERGENCY(1,"emergency"), - EQUIPMENT(2,"equipment"), - GOAL_MANAGE(3,"goal-manage"), - INCIDENT_MANAGE(4,"incident-manage"); - - private Integer moduleNo; - - private String moduleName; - - public Integer getModuleNo() { - return moduleNo; - } - - public void setModuleNo(Integer moduleNo) { - this.moduleNo = moduleNo; - } - - public String getModuleName() { - return moduleName; - } - - public void setModuleName(String moduleName) { - this.moduleName = moduleName; - } - - ModuleTypeEnums(Integer moduleNo, String moduleName) { - this.moduleNo = moduleNo; - this.moduleName = moduleName; - } - - public static ModuleTypeEnums getReviewStatus(Integer moduleNo) { - for (ModuleTypeEnums reviewStatus : ModuleTypeEnums.values()) { - if (reviewStatus.getModuleNo().equals(moduleNo)) { - return reviewStatus; - } - } - return null; - } -} diff --git a/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioDemo/service/FileService.java b/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioDemo/service/FileService.java deleted file mode 100644 index de006c3..0000000 --- a/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioDemo/service/FileService.java +++ /dev/null @@ -1,155 +0,0 @@ -package com.gkhy.safePlatform.minioDemo.service; - -import com.gkhy.safePlatform.commons.enums.ResultCodes; -import com.gkhy.safePlatform.commons.vo.ResultVO; -import com.gkhy.safePlatform.minioDemo.config.MinioConfig; -import com.gkhy.safePlatform.minioDemo.enums.ModuleTypeEnums; -import com.gkhy.safePlatform.minioDemo.utils.FileUtil; -import io.minio.GetPresignedObjectUrlArgs; -import io.minio.MinioClient; -import io.minio.errors.*; -import io.minio.http.Method; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; - -import javax.annotation.Resource; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.InputStream; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; -import java.text.SimpleDateFormat; -import java.util.*; - -import static com.gkhy.safePlatform.minioDemo.enums.ModuleTypeEnums.*; - -@Service -public class FileService { - - @Resource - private MinioConfig minioConfig; - - @Autowired - private MinioClient minioClient; - - @Resource - private FileUtil fileUtil; - - /** - * 文件下载 - * @param fileName - * @param response - */ - public void downloadFile(String fileName, HttpServletResponse response) { - fileUtil.downloadFile(fileName,response); - } - - /** - * 文件上传 - * @param file - * @param moduleType - * @return - */ - public ResultVO<String> uploadFile(MultipartFile file, Integer moduleType) { - String url = ""; - String moduleName = initModuleName(moduleType); - - // 文件名转换 - String time = new SimpleDateFormat("yyyyMMdd").format(new Date()); - String fileSuffixName = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".")+1); - String fileName = moduleName + "/" + time + "_" + initUUID()+"."+fileSuffixName; - try { - url=fileUtil.upload(file, fileName); - } catch (Exception e) { - e.printStackTrace(); - } - return new ResultVO<>(ResultCodes.OK, url); - } - - /** - * 模块名称转换 - */ - private String initModuleName(Integer moduleType){ - String moduleName = ""; - ModuleTypeEnums moduleTypeEnums = ModuleTypeEnums.getReviewStatus(moduleType); - assert moduleTypeEnums != null; - switch (moduleTypeEnums) { - case EMERGENCY: - moduleName = EMERGENCY.getModuleName(); - break; - case EQUIPMENT: - moduleName = EQUIPMENT.getModuleName(); - break; - case GOAL_MANAGE: - moduleName = GOAL_MANAGE.getModuleName(); - break; - case INCIDENT_MANAGE: - moduleName = INCIDENT_MANAGE.getModuleName(); - break; - default: - break; - } - return moduleName; - } - - /** - * 生成8位UUID - */ - private String initUUID() { - String[] chars = new String[]{"a", "b", "c", "d", "e", "f", - "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", - "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", - "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", - "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", - "W", "X", "Y", "Z"}; - StringBuffer shortBuffer = new StringBuffer(); - String uuid = UUID.randomUUID().toString().replace("-", ""); - for (int i = 0; i < 8; i++) { - String str = uuid.substring(i * 4, i * 4 + 4); - int x = Integer.parseInt(str, 16); - shortBuffer.append(chars[x % 0x3E]); - } - return shortBuffer.toString(); - } - - - - - - public String getPutUrl(String objName) { - String url = null; - try { - Map<String, String> reqParams = new HashMap<>(); - reqParams.put("response-content-type", "application/json"); - url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() - .method(Method.PUT) - .bucket(minioConfig.getBucketName()) - .object(objName) - .expiry(60 * 2) - .extraQueryParams(reqParams) - .build()); - } catch (ErrorResponseException e) { - e.printStackTrace(); - } catch (InsufficientDataException e) { - e.printStackTrace(); - } catch (InternalException e) { - e.printStackTrace(); - } catch (InvalidKeyException e) { - e.printStackTrace(); - } catch (InvalidResponseException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (XmlParserException e) { - e.printStackTrace(); - } catch (ServerException e) { - e.printStackTrace(); - } - return url; - } - - -} diff --git a/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioDemo/utils/FileUtil.java b/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioDemo/utils/FileUtil.java deleted file mode 100644 index 2036414..0000000 --- a/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioDemo/utils/FileUtil.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.gkhy.safePlatform.minioDemo.utils; - -import com.gkhy.safePlatform.minioDemo.config.MinioConfig; -import com.sun.org.apache.regexp.internal.RE; -import io.minio.*; -import io.minio.MinioClient; -import io.minio.errors.*; -import io.minio.http.Method; -import lombok.SneakyThrows; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.multipart.MultipartFile; - -import javax.annotation.Resource; -import javax.servlet.ServletOutputStream; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; - -@Configuration -public class FileUtil { - - @Resource - private MinioConfig minioConfig; - - @Autowired - private MinioClient minioClient; - - - /** - * 文件下载 - * @param fileName - * @param response - */ - public void downloadFile(String fileName, HttpServletResponse response) { - try { - InputStream file = minioClient.getObject(GetObjectArgs.builder() - .bucket(minioConfig.getBucketName()) - .object(fileName) - .build()); - String filename = new String(fileName.getBytes("ISO8859-1"), StandardCharsets.UTF_8); - response.setHeader("Content-Disposition", "attachment;filename=" + filename); - ServletOutputStream servletOutputStream = response.getOutputStream(); - int len; - byte[] buffer = new byte[1024]; - while ((len = file.read(buffer)) > 0) { - servletOutputStream.write(buffer, 0, len); - } - servletOutputStream.flush(); - file.close(); - servletOutputStream.close(); - } catch (ErrorResponseException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - - /** - * 文件上传 - * - * @param file 文件 - * @param fileName 存储的文件名称 - */ - public String upload(MultipartFile file, String fileName) { - String url; - try { - PutObjectArgs objectArgs = PutObjectArgs.builder() - .bucket(minioConfig.getBucketName()) - .object(fileName) - .stream(file.getInputStream(), file.getSize(), -1) - .contentType(file.getContentType()) - .build(); - //文件名称相同会覆盖 - minioClient.putObject(objectArgs); - url = getFileUrl(fileName); - } catch (Exception e) { - e.printStackTrace(); - url = ""; - } - return url; - } - - /** - * 文件访问路径 - * - * @param objectName 存储桶里的对象名称 - * expiry :链接失效时间,秒为单位 - * @return - */ - public String getFileUrl(String objectName) { - String url = null; - try { - url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() - .method(Method.GET) - .bucket(minioConfig.getBucketName()) - .object(objectName) - .expiry(60).build()); - } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException | InvalidResponseException | IOException | NoSuchAlgorithmException | XmlParserException | ServerException e) { - e.printStackTrace(); - } - return url; - } - - - /** - * 检查存储桶是否存在 - * - * @return - */ - @SneakyThrows - public boolean bucketCheck() { - boolean flag = minioClient.bucketExists(BucketExistsArgs.builder() - .bucket(minioConfig.getBucketName()).build()); - return flag; - } - -} diff --git a/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioDemo/config/MinioConfig.java b/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioFile/config/MinioConfig.java similarity index 97% rename from safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioDemo/config/MinioConfig.java rename to safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioFile/config/MinioConfig.java index ebfc4cb..25d2621 100644 --- a/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioDemo/config/MinioConfig.java +++ b/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioFile/config/MinioConfig.java @@ -1,4 +1,4 @@ -package com.gkhy.safePlatform.minioDemo.config; +package com.gkhy.safePlatform.minioFile.config; import io.minio.MinioClient; import org.springframework.boot.context.properties.ConfigurationProperties; diff --git a/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioFile/controller/MinioController.java b/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioFile/controller/MinioController.java new file mode 100644 index 0000000..d95e90b --- /dev/null +++ b/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioFile/controller/MinioController.java @@ -0,0 +1,27 @@ +package com.gkhy.safePlatform.minioFile.controller; + +import com.gkhy.safePlatform.minioFile.service.MinioAccessService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + + +@RestController +@RequestMapping(value = "/minio/file") +public class MinioController { + + @Autowired + private MinioAccessService minioAccessService; + + @RequestMapping(value = "/view",method = RequestMethod.GET) + public Object viewFile(String obj){ + return minioAccessService.viewFile(obj); + } + + @RequestMapping(value = "/presign",method = RequestMethod.GET) + public Object getPresignUrl(String prefixName,String suffixName){ + return minioAccessService.getPresignUrl(prefixName,suffixName); + } +} + diff --git a/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioFile/service/MinioAccessService.java b/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioFile/service/MinioAccessService.java new file mode 100644 index 0000000..2bc51f6 --- /dev/null +++ b/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioFile/service/MinioAccessService.java @@ -0,0 +1,97 @@ +package com.gkhy.safePlatform.minioFile.service; + +import com.gkhy.safePlatform.minioFile.config.MinioConfig; +import com.gkhy.safePlatform.minioFile.utils.UUIDUtil; +import io.minio.GetPresignedObjectUrlArgs; +import io.minio.MinioClient; +import io.minio.errors.*; +import io.minio.http.Method; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.io.IOException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +@Service +public class MinioAccessService { + + @Resource + private MinioConfig minioConfig; + + @Autowired + private MinioClient minioClient; + + public String viewFile(String objectName){ + String url = null; + try { + url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() + .method(Method.GET) + .bucket(minioConfig.getBucketName()) + .object(objectName) + .expiry(60).build()); + } catch (ErrorResponseException e) { + e.printStackTrace(); + } catch (InsufficientDataException e) { + e.printStackTrace(); + } catch (InternalException e) { + e.printStackTrace(); + } catch (InvalidKeyException e) { + e.printStackTrace(); + } catch (InvalidResponseException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (XmlParserException e) { + e.printStackTrace(); + } catch (ServerException e) { + e.printStackTrace(); + } + return url; + } + + public String getPresignUrl(String prefixName ,String suffixName){ + String uuid= UUIDUtil.initUUID(); + String objName = prefixName+uuid+suffixName; + String url = null; + try { + Map<String, String> reqParams = new HashMap<>(); + reqParams.put("response-content-type", "application/json"); + url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() + .method(Method.PUT) + .bucket(minioConfig.getBucketName()) + .object(objName) + .expiry(60*2) + .extraQueryParams(reqParams) + .build()); + } catch (ErrorResponseException e) { + e.printStackTrace(); + } catch (InsufficientDataException e) { + e.printStackTrace(); + } catch (InternalException e) { + e.printStackTrace(); + } catch (InvalidKeyException e) { + e.printStackTrace(); + } catch (InvalidResponseException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (XmlParserException e) { + e.printStackTrace(); + } catch (ServerException e) { + e.printStackTrace(); + } + return url; + } + + + +} diff --git a/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioFile/utils/UUIDUtil.java b/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioFile/utils/UUIDUtil.java new file mode 100644 index 0000000..a8737d4 --- /dev/null +++ b/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/minioFile/utils/UUIDUtil.java @@ -0,0 +1,25 @@ +package com.gkhy.safePlatform.minioFile.utils; + +import java.util.UUID; + +public class UUIDUtil { + /** + * 生成8位UUID + */ + public static String initUUID() { + String[] chars = new String[]{"a", "b", "c", "d", "e", "f", + "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", + "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", + "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", + "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", + "W", "X", "Y", "Z"}; + StringBuffer shortBuffer = new StringBuffer(); + String uuid = UUID.randomUUID().toString().replace("-", ""); + for (int i = 0; i < 8; i++) { + String str = uuid.substring(i * 4, i * 4 + 4); + int x = Integer.parseInt(str, 16); + shortBuffer.append(chars[x % 0x3E]); + } + return shortBuffer.toString(); + } +} -- Gitblit v1.9.2