該接口提供最爲資源讀取流的簡單接口。只有一個方法:
InputStream getInputStream()throwsIOException;
該方法每次調用都會返回一個新的流對象。
boolean exists(); boolean isReadable(); boolean isOpen(); URL getURL()throwsIOException; URI getURI()throwsIOException; File getFile()throwsIOException; long contentLength()throwsIOException; long lastModified()throwsIOException; Resource createRelative(String relativePath)throwsIOException; String getFilename(); String getDescription();
boolean isWritable(); OutputStream getOutputStream()throwsIOException;
String getPathWithinContext();
import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.springframework.core.io.ClassPathResource; public class ResourceTest { public static void main(String[] args) throws Exception { //applicationContext.xml,位於\WEB-INF\classes\spring\下 ClassPathResource cpr = new ClassPathResource("/spring/applicationContext.xml"); System.out.println(cpr.getFilename()); //SAX解析 SAXReader reader = new SAXReader(); Document doc = reader.read(cpr.getFile()); Element root = doc.getRootElement(); @SuppressWarnings("unchecked") List<Element> l = root.elements(); for(Element e : l){ System.out.println(e.asXML()); } } }
private final String path; private ClassLoader classLoader; private Class<?> clazz;
public ClassPathResource(String path) { this(path, (ClassLoader) null); }
public ClassPathResource(String path, ClassLoader classLoader) { Assert.notNull(path, "Path must not be null"); //處理傳入的路勁,好比\\置換爲 / String pathToUse = StringUtils.cleanPath(path); if (pathToUse.startsWith("/")) { pathToUse = pathToUse.substring(1); } this.path = pathToUse; //若是累加載器爲空,則獲取默認的類加載器。 this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader()); }
ClassPathResource cpr = new ClassPathResource("/spring/applicationContext.xml", ResourceTest.class.getClassLoader());
public final ClassLoader getClassLoader() { return (this.clazz != null ? this.clazz.getClassLoader() : this.classLoader); }
@Override public InputStream getInputStream() throws IOException { InputStream is; if (this.clazz != null) {//clazz對象是否爲空,不爲空經過clazz獲取輸入流 is = this.clazz.getResourceAsStream(this.path); } else if (this.classLoader != null) {//經過類加載器 is = this.classLoader.getResourceAsStream(this.path); } else { is = ClassLoader.getSystemResourceAsStream(this.path); } if (is == null) { throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist"); } return is; }
public String getDescription() { StringBuilder builder = new StringBuilder("class path resource ["); String pathToUse = path; if (this.clazz != null && !pathToUse.startsWith("/")) { builder.append(ClassUtils.classPackageAsResourcePath(this.clazz)); builder.append('/'); } if (pathToUse.startsWith("/")) { pathToUse = pathToUse.substring(1); } builder.append(pathToUse); builder.append(']'); return builder.toString(); }
@Override public File getFile() throws IOException { URL url = getURL();//獲取URL if (url.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) { return VfsResourceDelegate.getResource(url).getFile(); } return ResourceUtils.getFile(url, getDescription()); }
public URL getURL() throws IOException { URL url = resolveURL();//獲取URL if (url == null) { throw new FileNotFoundException(getDescription() + " cannot be resolved to URL because it does not exist"); } return url; }
protected URL resolveURL() { if (this.clazz != null) { return this.clazz.getResource(this.path); } else if (this.classLoader != null) { return this.classLoader.getResource(this.path); } else { return ClassLoader.getSystemResource(this.path); } }
public static void main(String[] args) { //applicationContext.xml放置於src目錄下 //直接訪問文件 FileSystemResource fsr = new FileSystemResource("/spring/applicationContext.xml"); System.out.println(fsr.getFile().exists());//false fsr = new FileSystemResource("src/applicationContext.xml"); System.out.println(fsr.getFile().exists());//true }
private final File file; private final String path;
public FileSystemResource(File file) { Assert.notNull(file, "File must not be null"); this.file = file; this.path = StringUtils.cleanPath(file.getPath()); }
public FileSystemResource(String path) { Assert.notNull(path, "Path must not be null"); this.file = new File(path); this.path = StringUtils.cleanPath(path); }
this.file = new File(path);
@Override public URL getURL() throws IOException { return this.file.toURI().toURL(); }
@Override public File getFile() { return this.file; }