Spring Designs A resource interface, which provides the application with greater access to the underlying resources. The interface has an implementation class that corresponds to a different resource type.
1) Boolean exists (): resource exists, 2) Boolean isOpen (): Resource is open, 3) URL GetURL () throws IOException: If the underlying resource can be represented as a URL, the method returns the corresponding URL object ; 4) File GetFile () throws IOException: if the underlying resource corresponds to a file, the method returns the corresponding files object; 5) InputStream getInputStream () throws IOException: Returns the input stream corresponding to the resource.
Sample Project structure diagram:
filesourceexample:
PackageCom.resource;ImportOrg.springframework.core.io.ClassPathResource;ImportOrg.springframework.core.io.FileSystemResource;ImportOrg.springframework.core.io.Resource;ImportOrg.springframework.core.io.support.EncodedResource;Importorg.springframework.util.FileCopyUtils;Importjava.io.IOException;ImportJava.io.InputStream;/*** Created by Gao on 16-3-18.*/ Public classFilesourceexample { Public Static voidMain (string[] args) {Try{String FilePath= "C:\\workspace\\projects\\chapter03\\web\\web-inf\\classes\\conf\\file1.txt"; //loading files using the System file path methodResource res1 =NewFilesystemresource (FilePath); //loading files using classpath modeResource Res2 =NewClasspathresource ("File1.txt"); Encodedresource Encres=NewEncodedresource (Res2, "UTF-8"); String content=filecopyutils.copytostring (Encres.getreader ()); SYSTEM.OUT.PRINTLN (content); InputStream ins1=Res1.getinputstream (); InputStream Ins2=Res2.getinputstream (); System.out.println ("Res1:" +res1.getfilename ()); System.out.println ("Res2:" +res2.getfilename ()); } Catch(IOException e) {e.printstacktrace (); } }}
Output Result:
Hahayou Get Me!!! Hellomy name is Liushizhen~~res1:file1.txtres2:file1.txt
idnex.jsp:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencodingimportimport =" Org.springframework.web.util.WebUtils "/><% new servletcontextresource (Application," \\WEB-INF\\classes\\conf\\file1.txt "); Out.print (Res3.getfilename ()+ "<br/>"); Out.print (Webutils.gettempdir (application). GetAbsolutePath ()); %>
Output Result:
Resource Access Tool