Spring之Resource
本文讲解如何使用Spring中的Resource接口处理资源
Resource接口的几个主要实现类如下:
-
UrlResource
-
ClassPathResource
-
FileSystemResource
-
PathResource
-
ServletContextResource
-
InputStreamResource
-
ByteArrayResource
UrlResource
访问本地资源
public static void main(String[] args) throws IOException {
Resource resource =
new UrlResource("file:\\C:\\data\\test.txt");
System.out.println(resource.getFile());
}
访问网络资源
使用springboot设置一个网络文本资源
访问网络资源需要通过getInputStream()获取流
public static void main(String[] args) throws IOException {
Resource resource =
new UrlResource("http://localhost:8080/test.txt");
InputStream is = resource.getInputStream();
byte[] txt = new byte[is.available()];
System.out.println(is.read(txt));
System.out.println(new String(txt));
}
也可以访问ftp协议资源(不便演示,做法相同)
ClassPathResource
此类表示应从类路径获取的资源。它使用线程上下文类加载器、给定的类加载器或给定的类来加载资源。
public static void main(String[] args) {
Resource resource = new ClassPathResource("test.txt");
System.out.println(resource.getFilename());
}
FileSystemResource
Spring 提供的 FileSystemResource 类用于访问文件系统资源,使用 FileSystemResource 来访问文件系统资源并没有太大的优势,因为 Java 提供的 File 类也可用于访问文件系统资源。
当然使用 FileSystemResource 也可消除底层资源访问的差异,程序通过统一的 Resource API 来进行资源访问。下面程序是使用 FileSystemResource 来访问文件系统资源的示例程序。
访问classpath下的资源
public static void main(String[] args) {
Resource resource = new FileSystemResource("test.txt");
System.out.println(resource.getFilename());
}
访问本地磁盘下的资源
public static void main(String[] args) throws IOException {
Resource resource = new FileSystemResource("c:\\data\\test.txt");
System.out.println(resource.getFile());
}
PathResource
读取本地资源
public static void main(String[] args) throws IOException {
Resource resource = new PathResource("c:\\data\\test.txt");
System.out.println(resource.getFile());
}
读取classpath下的资源
public static void main(String[] args) throws IOException {
Resource resource = new PathResource("test.txt");
System.out.println(resource.getFile());
}
ServletContextResource
该资源实现类作用是从web应用程序上下文中获取资源
创建一个SpringBoot接口如下:
@RestController
public class ServletContextResourceController {
@GetMapping("/test")
public String test(HttpServletRequest request) {
ServletContext servletContext = request.getServletContext();
Resource resource = new ServletContextResource(servletContext, "test.txt");
return resource.getFilename();
}
}
访问接口获取资源文件名返回
InputStreamResource
获取一个输入流,通过输入流获取资源
public static void main(String[] args) throws IOException {
// 获取文件对象
File file = new File("c:\\data\\test.txt");
// 获取文件输入流
FileInputStream fis = new FileInputStream(file);
// 模拟通过输入流
Resource resource = new InputStreamResource(fis);
System.out.println(resource.contentLength());
th());
}
ByteArrayResource
通过获取一个字节数组来加载资源
public static void main(String[] args) throws IOException {
String txt = "测试ByteArrayResource";
Resource resource = new ByteArrayResource(txt.getBytes(StandardCharsets.UTF_8));
System.out.println(resource.contentLength());
}
Spring之ResourceLoader
我们通过Spring的应用上下文获取资源加载加载各种方式的资源
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext();
Resource resource = ctx.getResource("classpath:text.txt");
System.out.println(resource.getFilename());
}
如上代码,实例化ApplicationContext,通过getResource()方法加载classpath类路径下的资源文件,classpath可以省略不写。
从文件系统中加载资源
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext();
// Resource resource = ctx.getResource("file:\\c:\\data\\test.txt");
Resource resource = ctx.getResource("c:\\data\\test.txt");
System.out.println(resource.getFilename());
}
加载(http)网络资源
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext();
Resource resource = ctx.getResource("http://lcoalhost:8080/test.txt");
System.out.println(resource.getFilename());
}
Spring之ResourcePatternResolver
从源码中得知,该接口继承了上一节中的ResourceLoader
源码如下:
public interface ResourcePatternResolver extends ResourceLoader {
String CLASSPATH_ALL_URL_PREFIX = "classpath*:";
Resource[] getResources(String locationPattern) throws IOException;
}
添加了一个getResources方法,能够获取多个Resource资源对象
而ApplicationContext接口又继承该接口,因此ApplicationContext接口也具备了加载资源的能力。
public static void main(String[] args) throws IOException {
ApplicationContext ctx = new ClassPathXmlApplicationContext();
Resource[] resources = ctx.getResources("test*.txt");
for (Resource resource : resources) {
System.out.println(resource.getFilename());
}
}
我们通过 * 通配符匹配test前缀的文件,都将加载进来,返回一个资源对象数组。
Spring之ResourceLoaderAware接口
Spring程序中,通过实现ResourceLoaderAware接口可以注入ResourceLoader资源加载器,而我们的ApplicationContext应用上下文继承了ResoucerLoader接口,因此他本身就是一个ResourceLoader资源加载器。
public class TestResourceLoaderAware {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
MyResource bean = ctx.getBean(MyResource.class);
System.out.println(bean.getResourceLoader());
}
}
@Configuration
class Config {
@Bean
public MyResource myResource() {
return new MyResource();
}
}
class MyResource implements ResourceLoaderAware {
private ResourceLoader resourceLoader;
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public ResourceLoader getResourceLoader() {
return resourceLoader;
}
}
如上述代码,我们创建MyResource类实现ResourceLoaderAware接口,获取ResourceLoader对象,应用上下通过加载Spring注解配置类创建ApplicationContext对象,获取MyResource这个Bean,并打印他的ResourceLoader属性,结果如下:
如图他就是我们的ApplicationContext对象