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

Files inside a war file can be read, not file the file path but via a classloader.

Question: How can I open and read a file via Java inside a war file of a web application?
Answer: InputStream can be used for this with a ClassLoader.

Implementation code

For web applications, the InputStream will be created using a ClassLoader. But this approach has one limitation.
We can read only the files inside WEB-INF/classes directory.
Below is the directory structure of our web application, look at the WEB-INF/classes directory that we are going to read files from.
import java.io.InputStream;

public class WebAppFileReader {

    public static void main(String[] args) throws Exception {

    // Full path to file that we need to open and read 
    // "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);
    // ....
    }
}

Not standard file reading mechanims?

Reading a file via it's path is not useful 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.
Commented line above, InputStream inputStream = new FileInputStream(filePath); shows the common approach used in non-web applications. Note that only one change has to be made to make it read the files inside a web application. This is highlighted below again.
// InputStream inputStream = new FileInputStream(filePath); // #1
   InputStream inputStream = 
                      WebAppFileReader.class.getClassLoader()
                            .getResourceAsStream(filePath); // #2

By replacing line #1 with #2, a class to read files of a web application is created.

Note: 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,2,Adsense,3,Ant,1,Apache,3,Axis,3,Blogger,1,Books,1,CentOS,2,Chrome,2,CSS,2,Database,3,Earn Online,3,Eclipse,10,Facebook,1,Firefox,10,Gmail,4,GNU/Linux,9,Google,26,GWT,8,Hardware,2,IE,5,Interesting,15,Internet,14,Java,49,Javascript,7,JBoss,1,Jenkins,1,Log4j,2,Me,6,Microsoft,2,Miscellaneous,1,News,11,Opinion,10,OSGi,1,PHP,1,Productivity,3,Programming,36,Puzzle,3,Security,4,Software,41,Sports,9,Spring,2,Story,6,Subversion,3,TDD,4,Tech,2,Tips,1,Tomcat,6,Tutorial,13,Ubuntu,4,Web application,14,Web Design,2,Web services,3,Windows,10,Yahoo,1,Zip,2,
ltr
item
Digizol: 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
Files inside a war file can be read, not file the file path but via a classloader.
https://4.bp.blogspot.com/-A_phoqIUFpc/WerjCDQoBUI/AAAAAAAACfA/JiRnPa1zhVo4Mgu-D4bI_aw6i-8TV3iWACLcBGAs/s1600/Open%2Band%2Bread%2Bany%2Bfile%2Bin%2Ba%2B.war%2Bfile%2Bof%2Ba%2Bweb%2Bapplication%2Bwith%2BJava%2Bwww.digizol.com.png
https://4.bp.blogspot.com/-A_phoqIUFpc/WerjCDQoBUI/AAAAAAAACfA/JiRnPa1zhVo4Mgu-D4bI_aw6i-8TV3iWACLcBGAs/s72-c/Open%2Band%2Bread%2Bany%2Bfile%2Bin%2Ba%2B.war%2Bfile%2Bof%2Ba%2Bweb%2Bapplication%2Bwith%2BJava%2Bwww.digizol.com.png
Digizol
http://www.digizol.com/2007/04/open-file-war-web-application-java.html
http://www.digizol.com/
http://www.digizol.com/
http://www.digizol.com/2007/04/open-file-war-web-application-java.html
true
7440473
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