FileAttachmentUtil.java
4.8 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
package com.pashanhoo.common.util.fileupload;
import polaris.fileattachment.AttachmentService;
import polaris.fileattachment.models.FileUrl;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FileAttachmentUtil {
private static final String FILE_URL_PATTERN = "^[^\\\\/:*?\"<>|\r\n]+?:[^\\\\/:*?\"<>|\r\n]+(/[^\\\\/:*?\"<>|\r\n]+)*$";
private static final Pattern DIRECTORY_NAME_PATTERN = Pattern.compile("([^/]*)[/]?$");
private static final Pattern SERVER_REGEX = Pattern.compile("^[^\\\\/:*?\"<>|\r\n]+?:");
private static final Pattern FILE_REGEX = Pattern.compile("[/:]([^\\\\/:*?\"<>|\r\n]+)$");
@Resource
private AttachmentService attachmentService;
public FileAttachmentUtil() {
}
public static FileUrl parseFileUrl(String fileUrl) throws RuntimeException {
int index1 = 0;
int index2 = 0;
if (fileUrl != null && fileUrl.matches(FILE_URL_PATTERN)) {
Matcher regexMatcher = SERVER_REGEX.matcher(fileUrl);
if (regexMatcher.find()) {
index1 = regexMatcher.end();
}
regexMatcher = FILE_REGEX.matcher(fileUrl);
if (regexMatcher.find()) {
index2 = regexMatcher.start(1);
}
if (index1 != 0 && index2 != 0 && index1 <= index2) {
return new FileUrl(fileUrl.substring(0, index1 - 1), fileUrl.substring(index1, index2), fileUrl.substring(index2));
} else {
throw new RuntimeException("附件url格式错误,fileUrl = " + fileUrl);
}
} else {
throw new RuntimeException("附件url格式错误,fileUrl = " + (fileUrl == null ? "null" : fileUrl));
}
}
public static String getServerNameFromFileUrl(String fileUrl) {
if (fileUrl != null && fileUrl.matches(FILE_URL_PATTERN)) {
Matcher regexMatcher = SERVER_REGEX.matcher(fileUrl);
if (regexMatcher.find()) {
int index = regexMatcher.end();
return fileUrl.substring(0, index - 1);
}
}
throw new RuntimeException("附件url格式错误,fileUrl = " + (fileUrl == null ? "null" : fileUrl));
}
public static String generateFileUrl(String serverName, String path, String fileId) throws RuntimeException {
if (serverName != null && !serverName.isEmpty()) {
if (path != null && !path.isEmpty()) {
if (fileId != null && !fileId.isEmpty()) {
StringBuilder result = new StringBuilder();
result.append(serverName);
result.append(":");
result.append(path);
if (!path.endsWith("/")) {
result.append("/");
}
result.append(fileId);
return result.toString();
} else {
throw new RuntimeException("创建附件服务器自定义格式的FileUrl地址时附件名称(ID)为null");
}
} else {
throw new RuntimeException("创建附件服务器自定义格式的FileUrl地址时附件路径为null");
}
} else {
throw new RuntimeException("创建附件服务器自定义格式的FileUrl地址时附件服务器名为null");
}
}
public static boolean isFileUrl(String fileUrl) {
return fileUrl.matches(FILE_URL_PATTERN);
}
public static String getParentPath(String path) {
Matcher m = DIRECTORY_NAME_PATTERN.matcher(path);
m.find();
return m.replaceAll("");
}
public static String getPathName(String path) {
Matcher m = DIRECTORY_NAME_PATTERN.matcher(path);
m.find();
return m.group(1);
}
public static String getSavePath(String bsm) {
String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
String path = filterString(bsm);
return date + "/" + path;
}
public static String filterString(String str) {
StringBuffer s = new StringBuffer(str.length());
for (int i = 0; i < str.length(); i++) {
if (!hasFullChar(String.valueOf(str.charAt(i)))) {
s.append(str.charAt(i));
}
}
return s.toString();
}
public static boolean hasFullChar(String str) {
return str.getBytes().length != str.length();
}
public static String getFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
}