首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

利用 Spring 中的 Resource 读取资料和网络资源

2012-10-30 
利用 Spring 中的 Resource 读取文件和网络资源利用 Spring 中的 Resource 读取文件和网络资源package com

利用 Spring 中的 Resource 读取文件和网络资源
利用 Spring 中的 Resource 读取文件和网络资源

package com.isoftstone.spring.beans;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Arrays;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.BeanFactoryAware;import org.springframework.beans.factory.InitializingBean;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.context.ResourceLoaderAware;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;import org.springframework.core.io.ResourceLoader;public class MyResource implements ApplicationContextAware, InitializingBean,                      BeanFactoryAware, ResourceLoaderAware{    private ApplicationContext ctx;    private BeanFactory beanFactory;    private ResourceLoader resourceLoader;    @Override    public void setApplicationContext(ApplicationContext ac)            throws BeansException    {        this.ctx = ac;    }    @Override    public void afterPropertiesSet() throws Exception    {        MyResource bean = (MyResource) ctx.getBean("myResource", MyResource.class);        bean.sayHello();        System.out.println("------------------------------");                bean = (MyResource) beanFactory.getBean("myResource", MyResource.class);        bean.sayHello();        System.out.println("------------------------------");                ClassPathResource resource = (ClassPathResource) resourceLoader.getResource("classpath:com/isoftstone/spring/beans/MyResource.class");        BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));        String line = reader.readLine();        for (; line != null; line = reader.readLine())        {            System.out.println(line);        }        System.out.println("--------------xxx----------------");                resource = (ClassPathResource) resourceLoader.getResource("classpath:com/isoftstone/spring/beans/MyResource.class");        InputStream is = resource.getInputStream();        byte[] buffer = new byte[1024];        int len = is.read(buffer);                for (; len > 0; len = is.read(buffer))        {            System.out.println(Arrays.toString(buffer));        }        System.out.println("---------------xxx---------------");                Resource urlr = resourceLoader.getResource("http://10.40.33.185/ci-master/home.do#");        BufferedReader reader1 = new BufferedReader(new InputStreamReader(urlr.getInputStream()));        String line1 = reader1.readLine();        for (; line1 != null; line1 = reader1.readLine())        {            System.out.println(new String(line1.getBytes(), "UTF-8"));        }    }        private final void sayHello()    {        System.out.println("Hello, World!");    }    @Override    public void setBeanFactory(BeanFactory beanFactory) throws BeansException    {        this.beanFactory = beanFactory;    }    @Override    public void setResourceLoader(ResourceLoader resourceLoader)    {        this.resourceLoader = resourceLoader;    }}

热点排行