-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResourceUtil.java
More file actions
255 lines (228 loc) · 8.61 KB
/
ResourceUtil.java
File metadata and controls
255 lines (228 loc) · 8.61 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package cn.sinobest.ypgj.util;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;
/**
* Author : lihaoquan
* Description : 读取配置文件类
*/
public class ResourceUtil {
/** classpath 前缀(用于url) */
public static final String CLASSPATH_URL_PREFIX = "classpath:";
/** URL prefix for loading from the file system: "file:" */
public static final String FILE_URL_PREFIX = "file:";
/** URL protocol for a file in the file system: "file" */
public static final String URL_PROTOCOL_FILE = "file";
/** URL protocol for an entry from a jar file: "jar" */
public static final String URL_PROTOCOL_JAR = "jar";
/** URL protocol for an entry from a zip file: "zip" */
public static final String URL_PROTOCOL_ZIP = "zip";
/** URL protocol for an entry from a WebSphere jar file: "wsjar" */
public static final String URL_PROTOCOL_WSJAR = "wsjar";
/** URL protocol for an entry from an OC4J jar file: "code-source" */
public static final String URL_PROTOCOL_CODE_SOURCE = "code-source";
/** Separator between JAR URL and file path within the JAR */
public static final String JAR_URL_SEPARATOR = "!/";
/**
* 获得一个 ClassLoader
*
* @return
*/
public static ClassLoader getClassLoader() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
} catch (Throwable ex) {
ex.printStackTrace();
}
if (cl == null) {
// No thread context class loader, use class loader of this class.
cl = ResourceUtil.class.getClassLoader();
}
return cl;
}
/**
* 获得配置文件的 url
*
* @param configFileName
* @return
* @throws Exception
*/
public static URL getUrl(String configFileName) throws Exception {
String path = configFileName;
if (configFileName.startsWith(CLASSPATH_URL_PREFIX)) {
path = configFileName.substring(CLASSPATH_URL_PREFIX.length());
}
URL url = getClassLoader().getResource(path);
if (url == null) {
String description = "file " + path + " ";
throw new FileNotFoundException(description + " cannot be found");
}
return url;
}
public static boolean isJarURL(URL url) {
String protocol = url.getProtocol();
return (URL_PROTOCOL_JAR.equals(protocol)
|| URL_PROTOCOL_ZIP.equals(protocol)
|| URL_PROTOCOL_WSJAR.equals(protocol) || (URL_PROTOCOL_CODE_SOURCE
.equals(protocol) && url.getPath().indexOf(JAR_URL_SEPARATOR) != -1));
}
public static URL extractJarFileURL(URL jarUrl)
throws MalformedURLException {
String urlFile = jarUrl.getFile();
int separatorIndex = urlFile.indexOf(JAR_URL_SEPARATOR);
if (separatorIndex != -1) {
String jarFile = urlFile.substring(0, separatorIndex);
try {
return new URL(jarFile);
} catch (MalformedURLException ex) {
// Probably no protocol in original jar URL, like
// "jar:C:/mypath/myjar.jar".
// This usually indicates that the jar file resides in the file
// system.
if (!jarFile.startsWith("/")) {
jarFile = "/" + jarFile;
}
return new URL(FILE_URL_PREFIX + jarFile);
}
} else {
return jarUrl;
}
}
/**
* 获得配置文件的 java.io.File
*
* @param file
* @return
* @throws Exception
*/
public static File getFile(String configFileName) throws Exception {
URL url = getUrl(configFileName);
// if (isJarURL(url)) {
// url = extractJarFileURL(url);
// }
return new File(toURI(url).getSchemeSpecificPart());
}
/**
* 获得配置文件的 java.io.InputStream
*
* @param configFileName
* @return
* @throws Exception
*/
public static InputStream getFileAsStream(String configFileName)
throws Exception {
String path = configFileName;
if (configFileName.startsWith(CLASSPATH_URL_PREFIX)) {
path = configFileName.substring(CLASSPATH_URL_PREFIX.length());
}
URL url = getUrl(path);
if (ResourceUtil.isJarURL(url)) {
url = ResourceUtil.extractJarFileURL(url);
JarFile currentJar = new JarFile(ResourceUtil.toURI(url)
.getSchemeSpecificPart());
JarEntry dbEntry = currentJar.getJarEntry(path);
return currentJar.getInputStream(dbEntry);
}
return new FileInputStream(new File(toURI(url).getSchemeSpecificPart()));
}
/**
* 读取文件
*
* @param filename
* @return
* @throws java.io.IOException
*/
public static String read(String filename) throws Exception {
InputStreamReader read = new InputStreamReader(
getFileAsStream(filename));
BufferedReader reader = new BufferedReader(read);
String line = "";
StringBuffer readfile = new StringBuffer();
while ((line = reader.readLine()) != null) {
readfile.append(line + "\r\n");
}
try {
if (read != null)
read.close();
if (reader != null)
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
return readfile.toString().trim();
}
public static URI toURI(URL url) throws URISyntaxException {
return new URI(url.toString().replaceAll(" ", "%20"));
}
/**
* 获得资源文件列表
*
* @param configFile
* - 1. 文件,支持通配符 2. 文件列表,以 "," 隔开
* @return
*/
public static File[] getResources(String configFiles) throws Exception {
String[] configFileArray = configFiles.split(",");
List<File> fileList = new ArrayList<File>(16);
for (int i = 0; i < configFileArray.length; i++) {
String configFile = configFileArray[i];
if (configFile.indexOf("*") >= 0) {
// 处理通配符
String config = configFile;
if (configFile.startsWith(CLASSPATH_URL_PREFIX)) {
config = configFile
.substring(CLASSPATH_URL_PREFIX.length());
}
int pos = config.lastIndexOf("/");
String path = null;
String fileName = null;
File searchPath = null;
if (pos >= 0) {
path = config.substring(0, pos);
searchPath = getFile(path);
fileName = config.substring(pos + 1);
} else {
searchPath = getFile("./");
fileName = config;
}
String s = fileName.replace('.', '#');
s = s.replaceAll("#", "\\\\.");
s = s.replace('*', '#');
s = s.replaceAll("#", ".*");
s = s.replace('?', '#');
s = s.replaceAll("#", ".?");
s = "^" + s + "$";
final Pattern pattern = Pattern.compile(s);
File[] files = searchPath.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return pattern.matcher(name).find();
}
});
if (files != null) {
for (File f : files) {
fileList.add(f);
}
}
} else {
fileList.add(getFile(configFile));
}
}
return fileList.toArray(new File[fileList.size()]);
}
public static void main(String[] args) throws Exception {
String configFile = "classpath:spring/app*.xml,classpath:spring/action*.xml";
File[] files = getResources(configFile);
System.out.println("files: " + (files == null ? "null" : files.length));
for (int i = 0; i < files.length; i++) {
System.out.println(files[i].getName());
}
}
}