I am trying to get a list of folders from the root directory in my JSP project and then display them to the user in a table, with links to child folders and the size of the folder. The code below is to get a list of directories inside the WebContent folder.
String absolutePath = this.getServletContext().getRealPath("/") + relativePath;
String fileList = "<tr><th>File Name</th><th>Size [B]</th><th>Last Updated</th></tr>";
File folder = new File(absolutePath);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isDirectory()) {
Path file = listOfFiles[i].toPath();
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
String time = attr.lastAccessTime().toString();
fileList += "<tr><td><img width='15px' alt='**' src='assets\\images\\folder.png'/> "
+ "<button onclick='navigate(this);'>" + listOfFiles[i].getName() + "</button></td><td>" + attr.size() + "</td><td>"
+ time.replace("T", " ").substring(0, time.indexOf("."))
+ "</td></tr>";
}
}
When I run the project locally, there is no problem. But after deploying my .war file on a server, when running I get a 500 error (null pointer exception) at line:
for (int i = 0; i < listOfFiles.length; i++)
Which tells me that the Folder array has not been initialized with the child folders.
I am assuming that I don't have the same access to these folders on the server as I do when I am running locally.
Any ideas how to access these folders? P.S. The server is running on Unix.