1.从类路径下加载
ClassLoader.getSystemResourceAsStream(path);
2.从指定URL加载
new URL(url).openStream();
3.从指定文件加载
new FileInputStream(path);
public static final String CLASSPATH_PREFIX = "classpath:";
public static final String URL_PREFIX = "url:";
public static final String FILE_PREFIX = "file:";
public static InputStream getInputStreamForPath(String resourcePath) throws IOException {
InputStream is;
if (resourcePath.startsWith(CLASSPATH_PREFIX)) {
is = loadFromClassPath(stripPrefix(resourcePath));
} else if (resourcePath.startsWith(URL_PREFIX)) {
is = loadFromUrl(stripPrefix(resourcePath));
} else if (resourcePath.startsWith(FILE_PREFIX)) {
is = loadFromFile(stripPrefix(resourcePath));
} else {
is = loadFromFile(resourcePath);
}
if (is == null) {
throw new IOException("Resource [" + resourcePath + "] could not be found.");
}
return is;
}