Total Pageviews

2015/12/04

[Fortify] Fix File Separator Issue

Problem

Code snippet:
 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
   /**
     * 複製檔案至新路徑
     * 
     * @param oldfile
     * @param status
     * @param fileName
     * @throws IOException
     */
    public void copyFile(File oldfile, String status, String fileName) throws IOException {
        SimpleDateFormat sdFormat = new SimpleDateFormat("yyyyMMdd");
        String date = sdFormat.format(new Date());
        // 20151130 fix fortify file separator issue
        String newPath = BACKUP_FILE_PATH + status + "\\" + date + "\\";
        File file = new File(newPath);

        InputStream inStream = null;
        FileOutputStream fs = null;

        try {
            if (!file.exists()) {
                file.mkdirs();
            }
            // int bytesum = 0;
            int byteread = 0;

            inStream = new FileInputStream(oldfile);// 讀入原檔
            fs = new FileOutputStream(newPath + fileName);
            byte[] buffer = new byte[1444];

            while ((byteread = inStream.read(buffer)) != -1) {
                // bytesum += byteread; 位元組數 檔案大小
                fs.write(buffer, 0, byteread);
            }

        } catch (Exception e) {
            log.info("複製檔案錯誤", e);
        } finally {
            if (fs != null) {
                fs.close();
            }

            if (inStream != null) {
                inStream.close();
            }
        }
    }

How-to
updated code snippet:
 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
    private final String FILE_SEPARATOR = FileSystems.getDefault().getSeparator();
 
    /**
     * 複製檔案至新路徑
     * 
     * @param oldfile
     * @param status
     * @param fileName
     * @throws IOException
     */
    public void copyFile(File oldfile, String status, String fileName) throws IOException {
        SimpleDateFormat sdFormat = new SimpleDateFormat("yyyyMMdd");
        String date = sdFormat.format(new Date());
        // 20151130 fix fortify file separator issue
        String newPath = BACKUP_FILE_PATH + status + FILE_SEPARATOR + date + FILE_SEPARATOR;
        File file = new File(newPath);

        InputStream inStream = null;
        FileOutputStream fs = null;

        try {
            if (!file.exists()) {
                file.mkdirs();
            }
            // int bytesum = 0;
            int byteread = 0;

            inStream = new FileInputStream(oldfile);// 讀入原檔
            fs = new FileOutputStream(newPath + fileName);
            byte[] buffer = new byte[1444];

            while ((byteread = inStream.read(buffer)) != -1) {
                // bytesum += byteread; 位元組數 檔案大小
                fs.write(buffer, 0, byteread);
            }

        } catch (Exception e) {
            log.info("複製檔案錯誤", e);
        } finally {
            if (fs != null) {
                fs.close();
            }

            if (inStream != null) {
                inStream.close();
            }
        }
    }


Reference
[1] http://stackoverflow.com/questions/8075373/file-separator-vs-filesystem-getseparator-vs-system-getpropertyfile-separato

No comments: