代码比较简单,先陈列如下:
package ch5.applicationContext.Resource;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.Resource;
public class TestSpring ...{
public static void main(String[] args) throws IOException...{
String realpath="";
//加载配置项
realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch5/applicationContext/Resource"+File.separator+"applicationContext.xml";
ApplicationContext applicationContext=new FileSystemXmlApplicationContext(realpath);
Resource res1=applicationContext.getResource("http://www.sohu.com");
Resource res2=applicationContext.getResource("file:///c:/aa.txt");
Resource res3=applicationContext.getResource("classpath:ch5/applicationContext/Resource/file.txt");
displayInfo(res1);
displayInfo(res2);
displayInfo(res3);
}
public static void displayInfo(Resource res) throws IOException...{
String s="";
DataInputStream in5 =
new DataInputStream(
new BufferedInputStream(res.getInputStream()));
BufferedReader in5br =
new BufferedReader(
new InputStreamReader(in5));
while((s=in5br.readLine())!=null)...{
System.out.println(s);
}
}
}
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.Resource;
public class TestSpring ...{
public static void main(String[] args) throws IOException...{
String realpath="";
//加载配置项
realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch5/applicationContext/Resource"+File.separator+"applicationContext.xml";
ApplicationContext applicationContext=new FileSystemXmlApplicationContext(realpath);
Resource res1=applicationContext.getResource("http://www.sohu.com");
Resource res2=applicationContext.getResource("file:///c:/aa.txt");
Resource res3=applicationContext.getResource("classpath:ch5/applicationContext/Resource/file.txt");
displayInfo(res1);
displayInfo(res2);
displayInfo(res3);
}
public static void displayInfo(Resource res) throws IOException...{
String s="";
DataInputStream in5 =
new DataInputStream(
new BufferedInputStream(res.getInputStream()));
BufferedReader in5br =
new BufferedReader(
new InputStreamReader(in5));
while((s=in5br.readLine())!=null)...{
System.out.println(s);
}
}
}
运行结果就是把三种协议解析出的内容打印出来
Spring支持三种协议File,http,Classpath,其中file和http都返回一个UrlResource实例,我们可以使用getFile,getInputStream方法得到文件流对象,进而解析,但是,当使用http协议时候,不能使用getFile方法,会有FileNotFoundException异常,建议之用getFileInputStream访问资源内容,因为他最有可能对所有可能存在的资源类型都有效
本文中的加载配置文件仅供延时,没有实际意义