UploadUtils.java
4.93 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.server.utils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;
public class UploadUtils {
/**
* 日期格式化对象
*/
public static final DateFormat MONTH_FORMAT = new SimpleDateFormat("/yyyyMM/ddHHmmss");
public static final DateFormat YEAR_MONTH_FORMAT = new SimpleDateFormat("yyyyMM");
public static String generateFilename(String path, String ext) {
String ret = path + MONTH_FORMAT.format(new Date()) + RandomStringUtils.random(4, Num62.N36_CHARS) + "." + ext;
return ret.replaceAll("/+", "/");
}
public static String generateMonthname() {
return YEAR_MONTH_FORMAT.format(new Date());
}
public static String generateByFilename(String path, String fileName, String ext) {
return path + fileName + "." + ext;
}
protected static final Pattern ILLEGAL_CURRENT_FOLDER_PATTERN = Pattern
.compile("^[^/]|[^/]$|/\\.{1,2}|\\\\|\\||:|\\?|\\*|\"|<|>|\\p{Cntrl}");
/**
* Sanitizes a filename from certain chars.<br />
* This method enforces the <code>forceSingleExtension</code> property and
* then replaces all occurrences of \, /, |, :, ?, *, ", <, >,
* control chars by _ (underscore).
*
* @param filename a potentially 'malicious' filename
* @return sanitized filename
*/
public static String sanitizeFileName(final String filename) {
if (StringUtils.isBlank(filename))
return filename;
String name = forceSingleExtension(filename);
// Remove \ / | : ? * " < > 'Control Chars' with _
return name.replaceAll("\\\\|/|\\||:|\\?|\\*|\"|<|>|\\p{Cntrl}", "_");
}
/**
* Sanitizes a folder name from certain chars.<br />
* This method replaces all occurrences of \, /, |, :, ?, *, ", <,
* >, control chars by _ (underscore).
*
* @param folderName a potentially 'malicious' folder name
* @return sanitized folder name
*/
public static String sanitizeFolderName(final String folderName) {
if (StringUtils.isBlank(folderName))
return folderName;
// Remove . \ / | : ? * " < > 'Control Chars' with _
return folderName.replaceAll(
"\\.|\\\\|/|\\||:|\\?|\\*|\"|<|>|\\p{Cntrl}", "_");
}
/**
* Checks whether a path complies with the FCKeditor File Browser <a href="http://docs.fckeditor.net/FCKeditor_2.x/Developers_Guide/Server_Side_Integration#File_Browser_Requests"
* target="_blank">rules</a>.
*
* @param path a potentially 'malicious' path
* @return <code>true</code> if path complies with the rules, else
* <code>false</code>
*/
public static boolean isValidPath(final String path) {
if(StringUtils.isBlank(path))
return false;
if (ILLEGAL_CURRENT_FOLDER_PATTERN.matcher(path).find())
return false;
return true;
}
/**
* Replaces all dots in a filename with underscores except the last one.
*
* @param filename filename to sanitize
* @return string with a single dot only
*/
public static String forceSingleExtension(final String filename) {
return filename.replaceAll("\\.(?![^.]+$)", "_");
}
/**
* Checks if a filename contains more than one dot.
*
* @param filename filename to check
* @return <code>true</code> if filename contains severals dots, else
* <code>false</code>
*/
public static boolean isSingleExtension(final String filename) {
return filename.matches("[^\\.]+\\.[^\\.]+");
}
/**
* Checks a directory for existence and creates it if non-existent.
*
* @param dir directory to check/create
*/
public static void checkDirAndCreate(File dir) {
if (!dir.exists())
dir.mkdirs();
}
/**
* Iterates over a base name and returns the first non-existent file.<br />
* This method extracts a file's base name, iterates over it until the first
* non-existent appearance with <code>basename(n).ext</code>. Where n is a
* positive integer starting from one.
*
* @param file base file
* @return first non-existent file
*/
public static File getUniqueFile(final File file) {
if (!file.exists())
return file;
File tmpFile = new File(file.getAbsolutePath());
File parentDir = tmpFile.getParentFile();
int count = 1;
String extension = FilenameUtils.getExtension(tmpFile.getName());
String baseName = FilenameUtils.getBaseName(tmpFile.getName());
do {
tmpFile = new File(parentDir, baseName + "(" + count++ + ")."
+ extension);
} while (tmpFile.exists());
return tmpFile;
}
}