Open and read any file in a .war file of a web application with Java

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 ClassLoad...

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.
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.

COMMENTS

BLOGGER: 16
Loading...
Name

About,8,Adsense,3,Ant,1,Apache,2,Axis,3,Database,2,Earn Online,3,Eclipse,8,Firefox,9,Google,27,GWT,8,Hardware,3,IE,1,Internet,1,Java,48,Javascript,8,Jenkins,1,Linux,6,Miscellaneous,18,News,46,Opinion,9,PHP,1,Productivity,3,Puzzle,3,Security,3,Software,25,Sports,9,Spring,2,TDD,4,Tech,87,Tips,18,Tomcat,5,Tools,19,Tutorial,13,Ubuntu,4,Web application,12,Web Design,2,Web services,4,Windows,3,
ltr
item
digizolm12: Open and read any file in a .war file of a web application with Java
Open and read any file in a .war file of a web application with Java
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlDYm_JBEqmedLRVpTMbQJEAxTAaZH_75oq34WGJvM-jU6RPpSmXIsoUVJ7b-AijIcD_exuIn-fF6JzdPjW5tSpDWrbVh08k4FuKR6-FL4F9q8ruer7hHCwFVPx_7FtKkGtXNl5W8K8Dxj/s320/read+files+in+web+applications.JPG
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlDYm_JBEqmedLRVpTMbQJEAxTAaZH_75oq34WGJvM-jU6RPpSmXIsoUVJ7b-AijIcD_exuIn-fF6JzdPjW5tSpDWrbVh08k4FuKR6-FL4F9q8ruer7hHCwFVPx_7FtKkGtXNl5W8K8Dxj/s72-c/read+files+in+web+applications.JPG
digizolm12
https://digizolm12.blogspot.com/2007/04/open-and-read-any-file-in-war-file-of.html
https://digizolm12.blogspot.com/
http://digizolm12.blogspot.com/
http://digizolm12.blogspot.com/2007/04/open-and-read-any-file-in-war-file-of.html
true
1218430210337039745
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy