前言
在流程流转过程中可能会需要附带一些文件,每个任务办理人都可以上传附件,然后在流转过程中可能会被多次审批。
本篇将介绍附件的添加、查询、下载和删除代码操作。
代码示例
老规矩,本文只贴activiti附件相关的代码,其它基础代码可以参看《SpringBoot2.0整合activiti6示例》
添加附件
activiti里添加附件有两种方式,文件方式url方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** * 添加附件(file文件) * @param file * @param taskId * @param processInstanceId * @param attachmentName * @param attachmentDescription * @throws IOException */ @PostMapping("/addAttachmentFile") public void addAttachment(@RequestParam("file")MultipartFile file, @RequestParam("taskId")String taskId, @RequestParam("processInstanceId")String processInstanceId, @RequestParam("attachmentName")String attachmentName, @RequestParam("attachmentDescription")String attachmentDescription) throws IOException { String attachmentType = file.getContentType() + ";" + FilenameUtils.getExtension(file.getOriginalFilename()); // 从session里获取当前登录用户 // identityService.setAuthenticatedUserId(""); taskService.createAttachment(attachmentType, taskId, processInstanceId, attachmentName, attachmentDescription , file.getInputStream()); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/** * 添加附件(url) * @param url * @param attachmentType * @param taskId * @param processInstanceId * @param attachmentName * @param attachmentDescription * @throws IOException */ @PostMapping("/addAttachmentUrl") public void addAttachment(@RequestParam("url")String url, @RequestParam("attachmentType")String attachmentType, @RequestParam("taskId")String taskId, @RequestParam("processInstanceId")String processInstanceId, @RequestParam("attachmentName")String attachmentName, @RequestParam("attachmentDescription")String attachmentDescription) throws IOException { // 从session里获取当前登录用户 // identityService.setAuthenticatedUserId(""); taskService.createAttachment(attachmentType, taskId, processInstanceId, attachmentName, attachmentDescription , url); } |
查询附件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * 查询某个task所关联的附件集合 * @param taskId * @return */ @GetMapping("taskAttachmentList") public List<Attachment> getTaskAttachments(String taskId){ return taskService.getTaskAttachments(taskId); } /** * 查询某个实例所关联的附件集合(多个task组合成一个实例) * @param processInstanceId * @return */ @GetMapping("instanceAttachmentList") public List<Attachment> getInstanceAttachments(String processInstanceId){ return taskService.getProcessInstanceAttachments(processInstanceId); } |
下载附件
1 2 3 4 5 6 7 8 9 10 11 |
@GetMapping("/downLoadAttachment/{attachmentId}") public void downLoadAttachment(@PathVariable("attachmentId")String attachmentId, HttpServletResponse response) throws IOException { Attachment attachment = taskService.getAttachment(attachmentId); InputStream attachmentContent = taskService.getAttachmentContent(attachmentId); String contentType = StringUtils.substringBefore(attachment.getType(), ";"); response.addHeader("Content-Type", contentType + ";charset=UTF-8"); String extensionFileName = StringUtils.substringAfter(attachment.getType(), ";"); String fileName = attachment.getName() + "." + extensionFileName; response.setHeader("Content-Disposition", "attachment; filename=" + fileName); IOUtils.copy(new BufferedInputStream(attachmentContent), response.getOutputStream()); } |
删除附件信息
1 2 3 4 5 6 7 8 9 |
/** * 根据附件ID删除附件信息 * @param attachmentId */ @GetMapping("deleteAttachment") public void deleteAttachment(String attachmentId){ // 如果有必要可先删除远程文件。deleleFile(remoteFileUrl) taskService.deleteAttachment(attachmentId); } |