【雜談Spring】Spring中Resource(資源)的獲取


通過Resource接口獲取資源

Resource接口的實現類有:

 

Resource接口繼承了InputStreamSource 接口,InputStreamSource 接口中有一個方法:getInputStream(),所以匯總起來,Resource接口中共有以下方法:

public class ResourceTest {

    /**
     * 使用ClassPathResource獲取資源
     **/
    @Test
    public void TestClassPath() throws IOException {
        Resource resource = new ClassPathResource("test.txt");

        //判斷文件是否存在:
        if (resource.exists()) {
            System.out.println("文件存在");
        }

        //判斷資源文件是否可讀
        if (resource.isReadable()) {
            System.out.println("文件可讀");
        }

        //判斷當前Resource代表的底層資源是否已經打開
        if (resource.isOpen()) {
            System.out.println("資源文件已打開");
        }

        System.out.println(resource.getURL());//獲取資源所在的URL
        System.out.println(resource.getURI());//獲取資源所在的URI
        resource.getFile();//返回當前資源對應的File。
        System.out.println(resource.contentLength());//輸出內容長度
        System.out.println(resource.lastModified());//返回當前Resource代表的底層資源的最后修改時間。
        resource.createRelative("MyFile");//根據資源的相對路徑創建新資源。[默認不支持創建相對路徑資源]
        System.out.println(resource.getFilename());//獲取資源文件名
        System.out.println(resource.getDescription());


        //獲取當前資源代表的輸入流
        if (resource.isReadable()) {
            InputStream is = resource.getInputStream();
            System.out.println(is);
            is.close();
        }

    }

    /**
     * 使用FileSystemResource獲取資源
     **/
    @Test
    public void TestFileSystem() throws IOException {
        Resource resource = new FileSystemResource("D:\\test.txt");
        System.out.println(resource.getFilename());
    }

    /**
     * 使用UrlResource獲取資源
     **/
    @Test
    public void TestUrl() throws MalformedURLException {
        Resource resource = new UrlResource("http://docs.spring.io/spring/docs/4.0.0.M1/spring-framework-reference/pdf/spring-framework-reference.pdf");
        System.out.println(resource.getFilename());
    }

    /**
     * 使用ByteArrayResource獲取字節數組封裝的資源
     **/
    @Test
    public void testByteArray() throws IOException {
        ByteArrayResource resource = new ByteArrayResource("Hello".getBytes());
        System.out.println(resource.getInputStream());

    }


    /**
     * 使用InputStreamResource獲取輸入流封裝的資源。針對於輸入流的Resource,其getInputStream()方法只能被調用一次。
     **/
    @Test
    public void testInputStream() throws Exception {
        InputStream is = new FileInputStream("D\\test.txt");
        InputStreamResource resource = new InputStreamResource(is);
        //對於InputStreamResource而言,其getInputStream()方法只能調用一次,繼續調用將拋出異常。
        InputStream is2 = resource.getInputStream();   //返回的就是構件時的那個InputStream
        System.out.println(is2);
        is.close();
    }
}

通過ResourceLoader接口獲取資源

Spring框架為了更方便的獲取資源,盡量弱化程序員對各個Resource接口的實現類的感知,定義了另一個ResourceLoader接口。該接口的getResource(String location)方法可以用來獲取資源。它的DefaultResourceLoader實現類可以適用於所有的環境,可以返回ClassPathResource、UrlResource等。

ResourceLoader在進行加載資源時需要使用前綴來指定需要加載:“classpath:path”表示返回ClasspathResource,“http://path”和“file:path”表示返回UrlResource資源,如果不加前綴則需要根據當前上下文來決定,DefaultResourceLoader默認實現是加載classpath資源。 

@Test
public void testResourceLoader() {

    ResourceLoader loader = new DefaultResourceLoader();
    Resource resource = loader.getResource("http://www.baidu.com");
    System.out.println(resource instanceof UrlResource); //true  
    resource = loader.getResource("classpath:test.txt");
    System.out.println(resource instanceof ClassPathResource); //true  
    resource = loader.getResource("test.txt");
    System.out.println(resource instanceof ClassPathResource); //true  

}

通過ApplicationContext獲取資源

所有ApplicationContext實例都實現了ResourceLoader接口,因此我們在使用Spring容器時,可以不去過於計較底層Resource的實現,也不需要自己創建Resource實現類,而是直接使用applicationContext.getResource(),獲取到bean容器本身的Resource,進而取到相關的資源信息。

public class MyResource implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void resource() throws IOException {
        Resource resource = applicationContext.getResource("file:D:\\test.txt");
        System.out.println(resource.getFilename());
        System.out.println(resource.contentLength());
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM