Spring資源抽象接口
?
JDK所提供的訪問資源的類(如java.net.URL、File等)并不能很好地滿足各種底層資源的訪問需求,比如缺少從類路徑或者Web容器的上下文獲取資源的操作類。有鑒于此,Spring設計了一個Resource接口,它為應用提供了更強的訪問底層資源的能力。該接口擁有對應不同資源類型的實現類。先來了解一下Resource接口的主要方法:
?
?
public interface InputStreamSource { //每次調用都將返回一個新鮮的資源對應的java.io. InputStream字節流, //調用者在使用完畢后必須關閉該資源。 InputStream getInputStream() throws IOException; }
?
?
public interface Resource extends InputStreamSource { //資源是否存在。 boolean exists(); //是否可以讀 boolean isReadable(); //資源是否打開 boolean isOpen(); //如果底層資源可以表示為urL,則返回url對象 URL getURL() throws IOException; URI getURI() throws IOException; //如果底層資源對應的一個文件,該方法返回對應的File對象 File getFile() throws IOException; //返回當前Resource代表的底層文件資源的長度,一般是值代表的文件資源的長度。 long contentLength() throws IOException; //最后修改時間 long lastModified() throws IOException; //通過path 得到一個Resource對象 Resource createRelative(String relativePath) throws IOException; //獲取資源名稱 String getFilename(); //資源描述 String getDescription(); }
?
Resource在spring框架中起著不可或缺的作用,spring框架使用Resource裝在各種資源,這些資源包括配置文件資源、國際化屬性文件資源等。下面我們來了解一下Resource的具體實現類,如下圖
?
1.ByteArrayResource:二進制數組表示的資源,二進制數組資源可以在內存中通過程序構造
?
2.ClassPathResource:類路徑下的資源
?
3.FileSystemResource: 文件系統資源
?
4.InputStreamResource: 輸入流返回表示資源
?
5.ServletContextResource: 基于web容器上下文資源
?
6.UrlResource: 對java.io.URL的封裝,可以表示很多的協議的網絡資源(http,ftp,文件系統資源)
?
有了這個抽象的資源類后,我們就可以將spring的配置信息放置在任何地方,只要最終可以通過Resource接口返回配置信息即可
?
7.PortletContextResource: 基于web容器上下文資源
?
8.DescriptiveResource:
?
假如有一個文件位于web應用的類路徑下,用戶可以通過以下方式對這個資源進行訪問:
?
1.通過FileSystemResource以文件系統絕對路徑的方式進行訪問
?
2.通過ClassPathResource以類路徑的方式進行訪問
?
3.通過ServletContextResource以相對于Web應用根目錄的方式進行訪問
?
下面,我們分別通過FileSystemResource和ClassPathResource訪問同一個文件資源:
?
?
package com.fortune.test.resource; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import java.io.IOException; import java.io.InputStream; /** * Created with IntelliJ IDEA. * User: Alan * Date: 12-5-18 * Time: 上午10:58 */ public class FileSourceExample { public static void main(String args[]){ try{ String filePath ="D:/maskerSpring/chapter3/WebRoot/WEB-INF/classes/conf/file1.txt"; //①使用系統文件路徑方式加載文件 Resource res1 = new FileSystemResource(filePath); //②使用類路徑方式加載文件 Resource res2 = new ClassPathResource("/conf/file1.txt"); 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(); } } }?
在獲得資源后,用戶就可以通過Resource接口定義的多個方法訪問文件的數據和其它的信息:
?
如可以通過getFileName()直接獲取文件的輸入流。此外,還可以通過createRelative(String relativePath)在資源相對地址上創建新的文件。
?
在Web應用中,用戶還可以通過ServletContextResource以相對于Web應用根目錄的方式訪問文件資源,如下所示:
?
?
<%@ page import="org.springframework.core.io.Resource" %> <%@ page import="org.springframework.web.context.support.ServletContextResource" %> <%@ page import="org.springframework.web.util.WebUtils" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% //①注意文件資源地址以相對于Web應用根路徑的方式表示 Resource res3 = new ServletContextResource(application,"/WEB-INF/classes/conf/file1.txt"); out.print(res3.getFilename()+"<br/>"); out.print(WebUtils.getTempDir(application).getAbsolutePath()); %>
?
對于位于遠程服務器(Web服務器或FTP服務器)的文件資源,用戶可以方便地通過UrlResource進行訪問。
?
資源加載時默認采用系統編碼讀取資源內容,如果資源文件采用特殊的編碼格式,那么可以通過EncodedResource對資源進行編碼,以保證資源內容操作的正確性:
?
?
package com.fortune.test.resource; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.EncodedResource; import org.springframework.util.FileCopyUtils; /** * Created with IntelliJ IDEA. * User: Alan * Date: 12-5-18 * Time: 下午12:15 */ public class EncodedResourceExample { public static void main(String args[]) throws Throwable { Resource res = new ClassPathResource("conf/file1.txt"); EncodedResource encRes = new EncodedResource(res, "UTF-8"); String content = FileCopyUtils.copyToString(encRes.getReader()); System.out.println(content); } }
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
