How-To
Sample code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | @PostMapping(value = "/{projectId}/downloadZIP", produces = "application/zip") public FileSystemResource downloadZIP(@PathVariable Integer projectId, Alerter alerter) throws Exception { String identifier = repo.getOne(projectId).getIdentifier(); DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String currentTime = dateFormat.format(new Date()); String fileName = identifier + "-" + currentTime; String domainJSON = projectService.getDomainJSON(projectId); String storyMD = projectService.getStoryMD(projectId); File tempFile = File.createTempFile(fileName, ".zip"); tempFile.deleteOnExit(); try (OutputStream fos = new FileOutputStream(tempFile); BufferedOutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zos = new ZipOutputStream(bos);) { zos.putNextEntry(new ZipEntry("domain-" + currentTime + ".json")); zos.write(domainJSON.getBytes()); zos.closeEntry(); zos.putNextEntry(new ZipEntry("story-" + currentTime + ".md")); zos.write(storyMD.getBytes()); zos.closeEntry(); } catch (IOException e) { throw new RuntimeException("檔案匯出失敗, 錯誤原因:" + e.getMessage(), e); } return new FileSystemResource(tempFile); } |
No comments:
Post a Comment