需使用commons-io的IOUtils工具类将InputStream转换成String 在pom.xml的中添加如下
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
sadfasdfasdfasdfasdfsad
sadfasdfasdfasdfasdfsad
sadfasdfasdfasdfasdfsad
package com.wisely.resource;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Component;
@Component
public class Main {
@Value("classpath:com/wisely/resource/info.txt")
private Resource info;
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.resource");
Main main = context.getBean(Main.class);
System.out.println(main.injectInfo());
System.out.println("----------------------------");
//classpath: spring的一个模拟协议,类似于http:
Resource file = context.getResource("classpath:com/wisely/resource/info.txt");
System.out.println(IOUtils.toString(file.getInputStream()));
System.out.println("----------------------------");
Resource url = (UrlResource) context.getResource("http://www.baidu.com");
System.out.println(IOUtils.toString(url.getInputStream()));
context.close();
}
public String injectInfo() throws IOException{
return IOUtils.toString(info.getInputStream());
}
}
输出结果
sadfasdfasdfasdfasdfsad
sadfasdfasdfasdfasdfsad
sadfasdfasdfasdfasdfsad
----------------------------
sadfasdfasdfasdfasdfsad
sadfasdfasdfasdfasdfsad
sadfasdfasdfasdfasdfsad
----------------------------
<!DOCTYPE html><!--STATUS OK--><html><head>
.......
原文:http://wiselyman.iteye.com/blog/2210666