Resource Access Tool

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.