Question: How can I open and read a file inside a war file of a web application?
Answer: InputStream can be used for this with a ClassLoader.
A code snippet for opening a file from Java for reading a file inside a web application is listed below. Commented line (InputStream inputStream = new FileInputStream(filePath);) shows the common approach used in non-web applications. Common approach is not usable with web applications (.war file) since it fails to find the files. Even though the correct relative path is provided, programs will face issues depending on the web server versions.
For web applications, the InputStream will be created using a ClassLoader. Following code snippet can be used for this requirement. But this approach has one limitation. This can read only the files inside WEB-INF/classes folder.
Note that only one change has to be made to make it read the files inside a web application. This is highlighted below.
By replacing line #1 with #2, a class to read files of a web application is created. One important point to note is: WebAppFileReader is the name of the class in which these codes will be written. So if a different class is used with these code snippet, keep in mind to alter this line and add the class name of that.
Hope this will help you.
Answer: InputStream can be used for this with a ClassLoader.
A code snippet for opening a file from Java for reading a file inside a web application is listed below. Commented line (InputStream inputStream = new FileInputStream(filePath);) shows the common approach used in non-web applications. Common approach is not usable with web applications (.war file) since it fails to find the files. Even though the correct relative path is provided, programs will face issues depending on the web server versions.
For web applications, the InputStream will be created using a ClassLoader. Following code snippet can be used for this requirement. But this approach has one limitation. This can read only the files inside WEB-INF/classes folder.
import java.io.InputStream;
public class WebAppFileReader {
public static void main(String[] args) throws Exception {
// full path: "C://projects//myWeb//WebRoot//WEB-INF//classes//testFile.txt";
String filePath = "testFile.txt";
// InputStream inputStream = new FileInputStream(filePath);
InputStream inputStream = WebAppFileReader.class.getClassLoader()
.getResourceAsStream(filePath);
int size = 10;
byte chars[] = new byte[size];
inputStream.read(chars);
String str = "";
for (int i = 0; i < size; i++) {
str += (char) chars[i];
}
System.out.println(str);
....
}
}
Note that only one change has to be made to make it read the files inside a web application. This is highlighted below.
1. InputStream inputStream = new FileInputStream(filePath);
2. InputStream inputStream = WebAppFileReader.class.getClassLoader()
.getResourceAsStream(filePath);
By replacing line #1 with #2, a class to read files of a web application is created. One important point to note is: WebAppFileReader is the name of the class in which these codes will be written. So if a different class is used with these code snippet, keep in mind to alter this line and add the class name of that.
Hope this will help you.
great stuff !
ReplyDeletereally nice, thanks for that information, was looking for it, hard to find
ReplyDeleteUnbelievably helpful, wish I had found this 40 hours ago!!!
ReplyDeleteHi i have multiple war file and am extracting the war file using ant build.xml and each war file has multiple classes.. now i have doubt how to get the list of war and then get the classes files.. please help me... now am giving the warfile path directly.. how to get all the warfilepath...By vijayabaskar
ReplyDeleteReally great. This is what I expected for long time. Thanks a lot.
ReplyDeleteGreat.
ReplyDeleteis there any way to read file outside the web application?
ReplyDeletethank you..
// read file outside the web application
ReplyDeleteWhat is the location that you are planning to read files from?
This article really helped us
ReplyDeleteWhere is this class located? looks like something useful but not if I cant find it.
ReplyDelete// 9. Not sure what you meant by where? As you can see in the example WebAppFileReader is a class written by the developer.
ReplyDeletethax that help a lot
ReplyDeletethanx that help a lot :D
ReplyDeleteThanks a lot !!!
ReplyDeleteFinally I read a file inside a WAR file...
Congratulations Kamal !!!
The screenshot with the circle on it was a nice touch.
ReplyDelete