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 retri
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();
}