<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 【第四章】 資源 之 4.1 基礎知識 ——跟我學spring3 ### 4.1.1? 概述 在日常程序開發中,處理外部資源是很繁瑣的事情,我們可能需要處理URL資源、File資源資源、ClassPath相關資源、服務器相關資源(JBoss AS 5.x上的VFS資源)等等很多資源。因此處理這些資源需要使用不同的接口,這就增加了我們系統的復雜性;而且處理這些資源步驟都是類似的(打開資源、讀取資源、關閉資源),因此如果能抽象出一個統一的接口來對這些底層資源進行統一訪問,是不是很方便,而且使我們系統更加簡潔,都是對不同的底層資源使用同一個接口進行訪問。 Spring 提供一個Resource接口來統一這些底層資源一致的訪問,而且提供了一些便利的接口,從而能提供我們的生產力。 ### 4.1.2? Resource接口 Spring的Resource接口代表底層外部資源,提供了對底層外部資源的一致性訪問接口。 ``` public interface InputStreamSource { InputStream getInputStream() throws IOException; } ``` ``` public interface Resource extends InputStreamSource { boolean exists(); boolean isReadable(); boolean isOpen(); URL getURL() throws IOException; URI getURI() throws IOException; File getFile() throws IOException; long contentLength() throws IOException; long lastModified() throws IOException; Resource createRelative(String relativePath) throws IOException; String getFilename(); String getDescription(); } ``` 1)InputStreamSource接口解析: **getInputStream:**每次調用都將返回一個新鮮的資源對應的java.io. InputStream字節流,調用者在使用完畢后必須關閉該資源。 2)Resource接口繼承InputStreamSource接口,并提供一些便利方法: **exists:**返回當前Resource代表的底層資源是否存在,true表示存在。 **isReadable:**返回當前Resource代表的底層資源是否可讀,true表示可讀。 **isOpen:**返回當前Resource代表的底層資源是否已經打開,如果返回true,則只能被讀取一次然后關閉以避免資源泄露;常見的Resource實現一般返回false。 **getURL:**如果當前Resource代表的底層資源能由java.util.URL代表,則返回該URL,否則拋出IOException。 **getURI:**如果當前Resource代表的底層資源能由java.util.URI代表,則返回該URI,否則拋出IOException。 **getFile:**如果當前Resource代表的底層資源能由java.io.File代表,則返回該File,否則拋出IOException。 **contentLength:**返回當前Resource代表的底層文件資源的長度,一般是值代表的文件資源的長度。 **lastModified:**返回當前Resource代表的底層資源的最后修改時間。 **createRelative:**用于創建相對于當前Resource代表的底層資源的資源,比如當前Resource代表文件資源“d:/test/”則createRelative(“test.txt”)將返回表文件資源“d:/test/test.txt”Resource資源。 **getFilename:**返回當前Resource代表的底層文件資源的文件路徑,比如File資源“file://d:/test.txt”將返回“d:/test.txt”,而URL資源http://www.javass.cn將返回“”,因為只返回文件路徑。 **getDescription:**返回當前Resource代表的底層資源的描述符,通常就是資源的全路徑(實際文件名或實際URL地址)。 Resource接口提供了足夠的抽象,足夠滿足我們日常使用。而且提供了很多內置Resource實現:ByteArrayResource、InputStreamResource 、FileSystemResource 、UrlResource 、ClassPathResource、ServletContextResource、VfsResource等。 原創內容 轉自請注明【[http://sishuok.com/forum/blogPost/list/0/2455.html](http://sishuok.com/forum/blogPost/list/0/2455.html#7103)】
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看