In my Spring 3.0 app, I have some resources in /WEB-INF/dir
. At runtime I need some of them as an InputStream
(or some other type). How can I retrieve them? Is it possible to inject them as a normal Resource
?

- 32,968
- 8
- 81
- 101

- 8,651
- 18
- 78
- 133
5 Answers
Here is an easiest way to do it via annotation:
import org.springframework.core.io.Resource;
@Value("classpath:<path to file>")
private Resource cert;

- 10,283
- 1
- 62
- 71
-
1Any idea on how can a directory be loaded? E.g, a directory "dir" which contains 25 files – Menelaos Kotsollaris May 05 '17 at 02:50
-
@Andrei_N, I believe I have this issue of not working in a jar file. If i run as spring boot application it works fine, but if I deploy a jar to aws beanstalk/ec2 i get exception. do you have reccomendation on how to handle in a jar – yasgur99 Jul 02 '19 at 19:25
All ApplicationContext
s are, by definition, ResourceLoader
s. This means that they are capable of resolving any resource strings found within their configuration. With this in mind, you can declare your target bean with a setter that accepts an org.springframework.core.io.Resource
. Then when you configure the target bean, just use a resource path in the value for the property. Spring will attempt to convert the String
value in your configuration into a Resource
.
public class Target {
private Resource resource;
public void setResource(final Resource resource) {
this.resource = resource;
}
}
//configuration
<beans>
<bean id="target" class="Target">
<property name="resource" value="classpath:path/to/file"/>
</bean>
</beans>

- 1
- 1

- 3,224
- 28
- 35
You should be able to use :
Resource resource = appContext.getResource("classpath:<your resource name>");
InputStream is = resource.getInputStream();
where appContext
is your Spring ApplicationContext
(specifically, a WebApplicationContext, since you have a webapp)

- 45,521
- 12
- 59
- 79
-
The `Resource` interface does not declare a `getInputStream()` method, you would need to call `new FileInputStream(resource.getFile())`. – Ryan Ransford Sep 20 '11 at 14:14
-
That's incorrect. `Resource` inherits it from the interface `org.springframework.core.io.InputStreamSource` – Saket Sep 20 '11 at 14:19
-
Here's a full example to retrieve a classpath resource. I use it to grab SQL files that have really complex queries which I don't want to store in Java classes:
public String getSqlFileContents(String fileName) {
StringBuffer sb = new StringBuffer();
try {
Resource resource = new ClassPathResource(fileName);
DataInputStream in = new DataInputStream(resource.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
sb.append(" " + strLine);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}

- 9,139
- 1
- 36
- 40
If you do not want to introduce dependency on Spring, follow approach detailed here: Populate Spring Bean's File field via Annotation

- 1
- 1

- 2,474
- 2
- 23
- 22
-
I fail to see how any of the answers to that question result in reduced dependency on Spring... – Ryan Ransford Sep 20 '11 at 14:18
-
-
1But annotations is the best way to achieve spring injection without violating do-not-repeat-yourself principle. The XML approach, that you recommend, forces you to repeat yourself. Sooner or later somebody is going to change the property name in the java file and forget to update the spring bean definition file. – Babu Subburathinam Sep 20 '11 at 16:37
-
The dependency nit might not be true, but this is a valid and very simple solution. – Karl the Pagan Jun 13 '13 at 00:13