Resources接口和实现类

Spring Resources概述

Java的标准iava.net.URL类和各种URL前缀的标准处理程序无法满足所有对low-evel资源的访问,比如: 没有标准化的URL实现可用于访问需要从类路径或相对于 ServletContext 获取的资源。并且缺少某些Spring所需要的功能,例如检测某资源是否存在等。而Spring的Resource声明了访问low-level资源的能力。


Resource接口


Spring的 Resource 接口位于org.springframework.core.io 中。旨在成为一个更强大的接口,用于抽象对低级资源的访问。以下显示了Resource接口定义的方法

public interface Resource extends InputStreamSource {
    boolean exist();
    
    boolean isReadable;
    
    boolean isOpen();
    
    boolean isFile();
    
    URL getURL() throws IOException;
    
    URL getURI() throws IOException;
    
    File getFile() throws IOException;
    
    ReadableByteChannel readableChannel() throws IOException;
    
    Resource createRelative(String relativePath) throws IOException;
    
    String getFilename();
    
    String getDescription();
    
}

UrlResource访问网络资源

1.创建模块

spring6-resource

2.引入依赖

    

        
            jakarta.annotation
            jakarta.annotation-api
            2.1.1
        

        
        
            org.springframework
            spring-context
            6.0.2
        


        
            org.springframework
            spring-aop
            6.0.2
        

        
            org.springframework
            spring-aspects
            6.0.2
        
        
        
            org.springframework
            spring-test
            6.0.2
        

        
        
            org.junit.jupiter
            junit-jupiter-api
            5.9.0
            test
        
        
            junit
            junit
            4.13.2
        

        
        
            org.apache.logging.log4j
            log4j-core
            2.19.0
        

        
            org.apache.logging.log4j
            log4j-slf4j2-impl
            2.19.0
        


    

3.创建包

com.yogurt.spring6.resource

4.创建类

UrlResourceDemo(演示UrlResource访问网络资源)

//UrlResource访问网络资源
public class UrlResourceDemo {
    public static void main(String[] args) {
        //访问前缀http
        loadUrlResource("http://www.baidu.com");
        //访问前缀file
        loadUrlResource("file:yogurt.txt");
    }

    //访问前缀http、file
    public static void loadUrlResource(String path){
        try {
        //创建Resource实现类的对象UrlResource
            UrlResource url = new UrlResource(path);
            //获取资源信息
            System.out.println(url.getFilename());
            System.out.println(url.getURI());
            System.out.println(url.getDescription());
            System.out.println(url.getInputStream().read());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

注意:yogurt.txt文件要在根路径下才可访问到。 

ClassPathResourceDemo(演示ClassPathResource访问类路径下资源)

//访问类路径下资源
public class ClassPathResourceDemo {

    public static void loadClasspathResource(String path){
        //创建对象ClassPathResource
        ClassPathResource resource = new ClassPathResource(path);

        System.out.println(resource.getFilename());
        System.out.println(resource.getDescription());
        //获取文件内容
        try {
            InputStream in = resource.getInputStream();
            byte[] b = new byte[1024];
            while (in.read(b) != -1){
                System.out.println(new String(b));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        loadClasspathResource("yogurt.txt");
    }
}

FileSystemResourceDemo(演示FileSystemResource访问类路径下资源)

//访问系统资源
public class FileSystemResourceDemo {
    public static void main(String[] args) {
        //绝对路径(要先在D盘中创建yogurt.txt文件)
        loadFileResource("d:\\yogurt.txt");

        //相对路径
        loadFileResource("yogurt.txt");
    }

    public static void loadFileResource(String path){
        //创建对象
        FileSystemResource resource = new FileSystemResource(path);

        System.out.println(resource.getFilename());
        System.out.println(resource.getDescription());
        try {
            InputStream in = resource.getInputStream();
            byte[] b = new byte[1024];
            while (in.read(b) != -1){
                System.out.println(new String(b));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

 Resource类圈

上述Resource实现类与Resource顶级接口的关系可以用下面的UML关系模型来表示

Resources接口和实现类_第1张图片

 

 

 

你可能感兴趣的:(java,mysql,开发语言)