`
yunchow
  • 浏览: 317656 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

利用 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;
    }
}


2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics