From e1d6dfad78afc05291067774d2e68c2fcde5ff68 Mon Sep 17 00:00:00 2001 From: bob Date: Wed, 18 Jan 2023 18:21:25 +0800 Subject: [PATCH] fix(common) fileUpload fileDownload --- .../common/AttachmentController.java | 80 +++++++++++++++ .../web/core/config/AttachmentConfig.java | 13 +++ .../src/main/resources/application.yml | 14 +-- .../controller/FlowHisTaskController.java | 33 ------- .../controller/SysAttachmentController.java | 73 ++++++++++++++ .../framework/web/service/TokenService.java | 20 ++++ .../ruoyi/system/domain/SysAttachment.java | 96 ++++++++++++++++++ .../system/mapper/SysAttachmentMapper.java | 62 ++++++++++++ .../system/service/ISysAttachmentService.java | 62 ++++++++++++ .../impl/SysAttachmentServiceImpl.java | 97 +++++++++++++++++++ .../mapper/system/SysAttachmentMapper.xml | 83 ++++++++++++++++ 11 files changed, 594 insertions(+), 39 deletions(-) create mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/AttachmentController.java create mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/core/config/AttachmentConfig.java delete mode 100644 ruoyi-flowable/src/main/java/com/ruoyi/flowable/controller/FlowHisTaskController.java create mode 100644 ruoyi-flowable/src/main/java/com/ruoyi/flowable/controller/SysAttachmentController.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/domain/SysAttachment.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysAttachmentMapper.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/service/ISysAttachmentService.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysAttachmentServiceImpl.java create mode 100644 ruoyi-system/src/main/resources/mapper/system/SysAttachmentMapper.xml diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/AttachmentController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/AttachmentController.java new file mode 100644 index 0000000..dcd682e --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/AttachmentController.java @@ -0,0 +1,80 @@ +package com.ruoyi.web.controller.common; + + +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.map.MapUtil; +import cn.hutool.core.util.RandomUtil; +import com.ruoyi.common.core.controller.BaseController; +import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.common.exception.CustomException; +import com.ruoyi.common.utils.ServletUtils; +import com.ruoyi.framework.web.service.TokenService; +import com.ruoyi.system.domain.SysAttachment; +import com.ruoyi.system.service.ISysAttachmentService; +import com.ruoyi.web.core.config.AttachmentConfig; +import lombok.extern.slf4j.Slf4j; +import org.joda.time.LocalDate; +import org.springframework.beans.factory.annotation.Autowired; +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.IOException; +import java.util.Date; + +/** + * 用户信息 + * + * @author ruoyi + */ +@Slf4j +@RestController +@RequestMapping("/common/attachment") +public class AttachmentController extends BaseController +{ + + @Autowired + private ISysAttachmentService attachmentService; + + @Autowired + private TokenService tokenService; + + @Autowired + private AttachmentConfig attachmentConfig; + + /** + * 获取用户列表 + */ + @PostMapping("/upload") + public AjaxResult upload(@RequestParam("file") MultipartFile file) { + String yearMonth = LocalDate.now().toString("yyyy/MM"); + + String fileName = RandomUtil.randomString(12)+"."+FileUtil.getSuffix(file.getOriginalFilename()); + String filePath = attachmentConfig.getPath() + FileUtil.FILE_SEPARATOR + yearMonth + + FileUtil.FILE_SEPARATOR + fileName; + try { + FileUtil.writeFromStream(file.getInputStream(), filePath); + } catch (IOException e) { + log.error("upload::writeFromStream::fail", e); + throw new CustomException("文件保存失败,请重试!"); + } + + SysAttachment attachment = new SysAttachment(); + attachment.setName(file.getOriginalFilename()); + attachment.setType(FileUtil.getSuffix(file.getOriginalFilename())); + attachment.setContentType(file.getContentType()); + attachment.setPath(filePath); + Date now = new Date(); + attachment.setCreateTime(now); + attachment.setUpdateTime(now); + attachment.setCreateBy(tokenService.getLoginUserName(ServletUtils.getRequest())); + int result = attachmentService.insertSysAttachment(attachment); + if (result < 1) { + throw new CustomException("文件上传失败,请重试!"); + } + return AjaxResult.success(MapUtil.of("id",attachment.getId())); + } + +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/AttachmentConfig.java b/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/AttachmentConfig.java new file mode 100644 index 0000000..01fafb6 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/AttachmentConfig.java @@ -0,0 +1,13 @@ +package com.ruoyi.web.core.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Data +@Component +@ConfigurationProperties(prefix = "attachment") +public class AttachmentConfig { + private String path; + +} diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index a6934e9..96b1db4 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -48,9 +48,9 @@ spring: servlet: multipart: # 单个文件大小 - max-file-size: 10MB + max-file-size: 100MB # 设置总上传的文件大小 - max-request-size: 20MB + max-request-size: 200MB # 服务模块 devtools: restart: @@ -87,7 +87,7 @@ token: secret: abcdefghijklmnopqrstuvwxyz # 令牌有效期(默认30分钟) expireTime: 30 - + # MyBatis配置 mybatis: # 搜索指定包别名 @@ -98,11 +98,11 @@ mybatis: configLocation: classpath:mybatis/mybatis-config.xml # PageHelper分页插件 -pagehelper: +pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true - params: count=countSql + params: count=countSql # Swagger配置 swagger: @@ -112,7 +112,7 @@ swagger: pathMapping: /dev-api # 防止XSS攻击 -xss: +xss: # 过滤开关 enabled: true # 排除链接(多个用逗号分隔) @@ -126,3 +126,5 @@ flowable: database-schema-update: false # 关闭定时任务JOB async-executor-activate: false +attachment: + path: /tmp diff --git a/ruoyi-flowable/src/main/java/com/ruoyi/flowable/controller/FlowHisTaskController.java b/ruoyi-flowable/src/main/java/com/ruoyi/flowable/controller/FlowHisTaskController.java deleted file mode 100644 index b1e10c9..0000000 --- a/ruoyi-flowable/src/main/java/com/ruoyi/flowable/controller/FlowHisTaskController.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.ruoyi.flowable.controller; - -import com.ruoyi.common.core.domain.AjaxResult; -import com.ruoyi.flowable.domain.dto.FlowTaskDto; -import com.ruoyi.flowable.service.IFlowInstanceService; -import com.ruoyi.flowable.service.IFlowTaskService; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; -import lombok.Data; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.*; - -import java.util.Map; - -@Slf4j -@Api(tags = "工作流流程任务管理") -@RestController -@RequestMapping("/flowable/hisTask") -public class FlowHisTaskController { - - @Autowired - private IFlowTaskService flowTaskService; - - @ApiOperation(value = "我发起的流程", response = FlowTaskDto.class) - @GetMapping(value = "/myProcess") - public AjaxResult myProcess(@ApiParam(value = "当前页码", required = true) @RequestParam Integer pageNum, - @ApiParam(value = "每页条数", required = true) @RequestParam Integer pageSize) { - return flowTaskService.myProcess(pageNum, pageSize); - } -} diff --git a/ruoyi-flowable/src/main/java/com/ruoyi/flowable/controller/SysAttachmentController.java b/ruoyi-flowable/src/main/java/com/ruoyi/flowable/controller/SysAttachmentController.java new file mode 100644 index 0000000..ff477f3 --- /dev/null +++ b/ruoyi-flowable/src/main/java/com/ruoyi/flowable/controller/SysAttachmentController.java @@ -0,0 +1,73 @@ +package com.ruoyi.flowable.controller; + +import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.core.util.ZipUtil; +import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.system.domain.SysAttachment; +import com.ruoyi.system.service.ISysAttachmentService; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.flowable.engine.RuntimeService; +import org.flowable.engine.runtime.Execution; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletResponse; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +@RestController +@RequestMapping("/flowable/attachment") +public class SysAttachmentController { + + @Autowired + private ISysAttachmentService attachmentService; + @Autowired + private RuntimeService runtimeService; + + /** + * 查询流程表单列表 + */ + @GetMapping("/download") + public AjaxResult download(@RequestParam String processInstanceId, HttpServletResponse response) throws IOException { + if (StringUtils.isBlank(processInstanceId)) { + return AjaxResult.error("附件下载失败,参数无效"); + } + Execution execution = runtimeService.createExecutionQuery().processInstanceId(processInstanceId).variableExists("attachmentIds").singleResult(); + if (Objects.isNull(execution)) { + return AjaxResult.error("该流程无附件!"); + } + Object attachmentIds = runtimeService.getVariable(execution.getId(), "attachmentIds"); + if (StrUtil.isEmptyIfStr(attachmentIds)) { + return AjaxResult.error("该流程无附件!"); + } + List ids = StrUtil.split(attachmentIds.toString(), ',', -1, true, Long::parseLong); + List attachments = ids.stream().map(e -> attachmentService.selectSysAttachmentById(e)).collect(Collectors.toList()); + if (CollectionUtils.isEmpty(attachments)) { + return AjaxResult.error("下载失败,附件无效!"); + } + List fileNames = attachments.stream().map(e -> e.getName()).collect(Collectors.toList()); + List fileInputStreams = attachments.stream().map(e -> e.getPath()).map(e -> { + try { + return new FileInputStream(e); + } catch (FileNotFoundException ex) { + throw new RuntimeException(ex); + } + }).collect(Collectors.toList()); + response.setHeader("content-type", "application/octet-stream"); + response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); + response.setContentType("application/octet-stream"); + response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=download.zip"); + ZipUtil.zip(response.getOutputStream(), ArrayUtil.toArray(fileNames, String.class), ArrayUtil.toArray(fileInputStreams, FileInputStream.class)); + return null; + } +} diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java index 0023855..200e27d 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java @@ -69,6 +69,26 @@ public class TokenService } return null; } + /** + * 获取用户身份信息 + * + * @return 用户信息 + */ + public String getLoginUserName(HttpServletRequest request) + { + // 获取请求携带的令牌 + String token = getToken(request); + if (StringUtils.isNotEmpty(token)) + { + Claims claims = parseToken(token); + // 解析对应的权限以及用户信息 + String uuid = (String) claims.get(Constants.LOGIN_USER_KEY); + String userKey = getTokenKey(uuid); + LoginUser user = redisCache.getCacheObject(userKey); + return user.getUsername(); + } + return ""; + } /** * 设置用户身份信息 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysAttachment.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysAttachment.java new file mode 100644 index 0000000..bbe8d63 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysAttachment.java @@ -0,0 +1,96 @@ +package com.ruoyi.system.domain; + +import com.ruoyi.common.annotation.Excel; +import com.ruoyi.common.core.domain.BaseEntity; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + +/** + * 【请填写功能名称】对象 sys_attachment + * + * @author ruoyi + * @date 2023-01-18 + */ +public class SysAttachment extends BaseEntity +{ + private static final long serialVersionUID = 1L; + + /** $column.columnComment */ + private Long id; + + /** 文件名 */ + @Excel(name = "文件名") + private String name; + + /** 文件类型 */ + @Excel(name = "文件类型") + private String type; + + /** 内容类型 */ + @Excel(name = "内容类型") + private String contentType; + + /** 文件路径 */ + @Excel(name = "文件路径") + private String path; + + public void setId(Long id) + { + this.id = id; + } + + public Long getId() + { + return id; + } + public void setName(String name) + { + this.name = name; + } + + public String getName() + { + return name; + } + public void setType(String type) + { + this.type = type; + } + + public String getType() + { + return type; + } + public void setContentType(String contentType) + { + this.contentType = contentType; + } + + public String getContentType() + { + return contentType; + } + public void setPath(String path) + { + this.path = path; + } + + public String getPath() + { + return path; + } + + @Override + public String toString() { + return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) + .append("id", getId()) + .append("name", getName()) + .append("type", getType()) + .append("contentType", getContentType()) + .append("path", getPath()) + .append("createTime", getCreateTime()) + .append("updateTime", getUpdateTime()) + .append("createBy", getCreateBy()) + .toString(); + } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysAttachmentMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysAttachmentMapper.java new file mode 100644 index 0000000..2015c51 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysAttachmentMapper.java @@ -0,0 +1,62 @@ +package com.ruoyi.system.mapper; + +import com.ruoyi.system.domain.SysAttachment; + +import java.util.List; + +/** + * 【请填写功能名称】Mapper接口 + * + * @author ruoyi + * @date 2023-01-18 + */ +public interface SysAttachmentMapper +{ + /** + * 查询【请填写功能名称】 + * + * @param id 【请填写功能名称】ID + * @return 【请填写功能名称】 + */ + public SysAttachment selectSysAttachmentById(Long id); + + /** + * 查询【请填写功能名称】列表 + * + * @param sysAttachment 【请填写功能名称】 + * @return 【请填写功能名称】集合 + */ + public List selectSysAttachmentList(SysAttachment sysAttachment); + + /** + * 新增【请填写功能名称】 + * + * @param sysAttachment 【请填写功能名称】 + * @return 结果 + */ + public int insertSysAttachment(SysAttachment sysAttachment); + + /** + * 修改【请填写功能名称】 + * + * @param sysAttachment 【请填写功能名称】 + * @return 结果 + */ + public int updateSysAttachment(SysAttachment sysAttachment); + + /** + * 删除【请填写功能名称】 + * + * @param id 【请填写功能名称】ID + * @return 结果 + */ + public int deleteSysAttachmentById(Long id); + + /** + * 批量删除【请填写功能名称】 + * + * @param ids 需要删除的数据ID + * @return 结果 + */ + public int deleteSysAttachmentByIds(Long[] ids); +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysAttachmentService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysAttachmentService.java new file mode 100644 index 0000000..3e41393 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysAttachmentService.java @@ -0,0 +1,62 @@ +package com.ruoyi.system.service; + +import com.ruoyi.system.domain.SysAttachment; + +import java.util.List; + +/** + * 【请填写功能名称】Service接口 + * + * @author ruoyi + * @date 2023-01-18 + */ +public interface ISysAttachmentService +{ + /** + * 查询【请填写功能名称】 + * + * @param id 【请填写功能名称】ID + * @return 【请填写功能名称】 + */ + public SysAttachment selectSysAttachmentById(Long id); + + /** + * 查询【请填写功能名称】列表 + * + * @param sysAttachment 【请填写功能名称】 + * @return 【请填写功能名称】集合 + */ + public List selectSysAttachmentList(SysAttachment sysAttachment); + + /** + * 新增【请填写功能名称】 + * + * @param sysAttachment 【请填写功能名称】 + * @return 结果 + */ + public int insertSysAttachment(SysAttachment sysAttachment); + + /** + * 修改【请填写功能名称】 + * + * @param sysAttachment 【请填写功能名称】 + * @return 结果 + */ + public int updateSysAttachment(SysAttachment sysAttachment); + + /** + * 批量删除【请填写功能名称】 + * + * @param ids 需要删除的【请填写功能名称】ID + * @return 结果 + */ + public int deleteSysAttachmentByIds(Long[] ids); + + /** + * 删除【请填写功能名称】信息 + * + * @param id 【请填写功能名称】ID + * @return 结果 + */ + public int deleteSysAttachmentById(Long id); +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysAttachmentServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysAttachmentServiceImpl.java new file mode 100644 index 0000000..f33f289 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysAttachmentServiceImpl.java @@ -0,0 +1,97 @@ +package com.ruoyi.system.service.impl; + +import com.ruoyi.common.utils.DateUtils; +import com.ruoyi.system.domain.SysAttachment; +import com.ruoyi.system.mapper.SysAttachmentMapper; +import com.ruoyi.system.service.ISysAttachmentService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * 【请填写功能名称】Service业务层处理 + * + * @author ruoyi + * @date 2023-01-18 + */ +@Slf4j +@Service +public class SysAttachmentServiceImpl implements ISysAttachmentService +{ + @Autowired + private SysAttachmentMapper sysAttachmentMapper; + /** + * 查询【请填写功能名称】 + * + * @param id 【请填写功能名称】ID + * @return 【请填写功能名称】 + */ + @Override + public SysAttachment selectSysAttachmentById(Long id) + { + return sysAttachmentMapper.selectSysAttachmentById(id); + } + /** + * 查询【请填写功能名称】列表 + * + * @param sysAttachment 【请填写功能名称】 + * @return 【请填写功能名称】 + */ + @Override + public List selectSysAttachmentList(SysAttachment sysAttachment) + { + return sysAttachmentMapper.selectSysAttachmentList(sysAttachment); + } + + /** + * 新增【请填写功能名称】 + * + * @param sysAttachment 【请填写功能名称】 + * @return 结果 + */ + @Override + public int insertSysAttachment(SysAttachment sysAttachment) + { + sysAttachment.setCreateTime(DateUtils.getNowDate()); + return sysAttachmentMapper.insertSysAttachment(sysAttachment); + } + + /** + * 修改【请填写功能名称】 + * + * @param sysAttachment 【请填写功能名称】 + * @return 结果 + */ + @Override + public int updateSysAttachment(SysAttachment sysAttachment) + { + sysAttachment.setUpdateTime(DateUtils.getNowDate()); + return sysAttachmentMapper.updateSysAttachment(sysAttachment); + } + + /** + * 批量删除【请填写功能名称】 + * + * @param ids 需要删除的【请填写功能名称】ID + * @return 结果 + */ + @Override + public int deleteSysAttachmentByIds(Long[] ids) + { + return sysAttachmentMapper.deleteSysAttachmentByIds(ids); + } + + /** + * 删除【请填写功能名称】信息 + * + * @param id 【请填写功能名称】ID + * @return 结果 + */ + @Override + public int deleteSysAttachmentById(Long id) + { + return sysAttachmentMapper.deleteSysAttachmentById(id); + } +} diff --git a/ruoyi-system/src/main/resources/mapper/system/SysAttachmentMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysAttachmentMapper.xml new file mode 100644 index 0000000..939c2ae --- /dev/null +++ b/ruoyi-system/src/main/resources/mapper/system/SysAttachmentMapper.xml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + select id, name, type, content_type, path, create_time, update_time, create_by from sys_attachment + + + + + + + + insert into sys_attachment + + name, + type, + content_type, + path, + create_time, + update_time, + create_by, + + + #{name}, + #{type}, + #{contentType}, + #{path}, + #{createTime}, + #{updateTime}, + #{createBy}, + + + + + update sys_attachment + + name = #{name}, + type = #{type}, + content_type = #{contentType}, + path = #{path}, + create_time = #{createTime}, + update_time = #{updateTime}, + create_by = #{createBy}, + + where id = #{id} + + + + delete from sys_attachment where id = #{id} + + + + delete from sys_attachment where id in + + #{id} + + +