Resource
针对于资源文件的统一接口
Resource
-UrlResource:URL对应的资源,根据一个URL地址即可构建
-ClassPathResource:获取类路径下的资源文件
-FileSystemResource:获取文件系统里面的资源
-ServletContextResource:ServletContext封装的资源,用于访问ServletContext环境下的资源
-InputStreamResource:针对于输入流封装的资源
-ByteArrayResource:针对于字节数组封装的资源
例子:
Resource
package com.txr.resourceStudy;
import java.io.IOException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
public class ResourceStudy implements ApplicationContextAware{
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
this.applicationContext =arg0;
}
public void resource() throws IOException
{
Resource resource = applicationContext.getResource("a.txt");
System.out.println(resource.getFilename());
System.out.println(resource.contentLength());
}
}
测试
@Test
public void testResource()
{
ResourceStudy resourceStudy=(ResourceStudy)context.getBean("resource");
try {
resourceStudy.resource();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
测试结果
a.txt
16
a.txt文件内容