Total Pageviews

2018/09/07

[Spring MVC] Upload ZIP file to server

Scenario

The file structure in ZIP file looks like:


How-To
Here has 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    @Transactional
    @PostMapping(value = "/{projectId}/importFile/{fileType}", produces = MediaType.APPLICATION_JSON_VALUE)
    public void importFile(@PathVariable Integer projectId, @PathVariable String fileType,
            @RequestParam(value = "file", required = true) MultipartFile file) throws IOException {

        Project project = repo.findOne(projectId);

        File tmpFile = null;
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String currentTime = dateFormat.format(new Date());
        try {
            tmpFile = File.createTempFile(file.getName() + "_" + currentTime, ".zip");
            tmpFile.deleteOnExit();
        } catch (IOException e) {
            throw new RuntimeException("建立 temp file 發生錯誤, 錯誤原因: " + e.getMessage(), e);
        }

        InputStream inputStream = null;
        ZipFile zipFile = null;
        try {
            inputStream = file.getInputStream();
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);

            Files.write(buffer, tmpFile);

            zipFile = new ZipFile(tmpFile);

            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();

                saveDomainJSON(project, entry, zipFile);
                saveStoryMD(project, entry, zipFile);
            }

            repo.updatePersistModelToNull(projectId);
        } catch (IOException e) {
            throw new RuntimeException("讀取 zip file 發生錯誤, 錯誤原因: " + e.getMessage(), e);
        } finally {
            if (zipFile != null) {
                zipFile.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }    
 
    private void saveDomainJSON(Project project, ZipEntry entry, ZipFile zipFile) throws IOException {
        if (entry.getName().startsWith("domain")) {
            try (InputStream inputSteam = zipFile.getInputStream(entry);) {
                String content = IOUtils.toString(inputSteam, org.apache.commons.io.Charsets.UTF_8);
                // save content into database
            } catch (IOException e) {
                throw new RuntimeException("讀取 domain JSON file 發生錯誤, 錯誤原因: " + e.getMessage(), e);
            }
        }  
    }
 
    private void saveStoryMD(Project project, ZipEntry entry, ZipFile zipFile) throws IOException {
        if (entry.getName().startsWith("story")) {
            try (InputStream inputSteam = zipFile.getInputStream(entry);) {
                String content = IOUtils.toString(inputSteam);
                // save content into database
            } catch (IOException e) {
                throw new RuntimeException("讀取 story Markdown file 發生錯誤, 錯誤原因: " + e.getMessage(), e);
            }
        }  
    }




No comments: