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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值