1

In a Java web application, how I can get a list of all files stored in a specific folder and pass them to the JSP?

I tried to do the following:

I stored all files in a folder images under WebContent In the Controller, I created a list of strings to which I added the URL of each file the Controller forwards to the JSP, that prints out the content of this folder in the View This is my code:

Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
File imagesFolder = new File("/uploading images/WebContent/images");
File[] imagesArray = imagesFolder.listFiles();
ArrayList<String> listOfImages = new ArrayList<String>();
for (File file : imagesArray) {
    listOfImages.add(file.toString());
}

request.setAttribute("listOfImages", listOfImages);
request.getRequestDispatcher("/elencoImmagini.jsp").forward(request, response);

}

JSP:

<%
ArrayList<String> listOfImages = (ArrayList<String>) request.getAttribute("listOfImages");

for (String image : listOfImages) {
    out.println(image);
}  

%>

Apparently there is something wrong with my code, as I get a NullPointerException for this line of the servlet:

for (File file : imagesArray) {

Initially I thought I passed an invalid path to imagesFolder, but I tried to change it several times and I always get the same error.

Where is the problem in your opinion?

EDIT: here is the error message:

SEVERE: Servlet.service() for servlet [controller.caricaImmagini] in context with path [/uploading_images] threw exception
java.lang.NullPointerException
    at controller.caricaImmagini.doGet(caricaImmagini.java:44)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Alex Reds
  • 197
  • 2
  • 4
  • 17
  • Your real problem is answered here: http://stackoverflow.com/questions/18664579/recommended-way-to-save-uploaded-files-in-a-servlet-application All answers posted so far below won't solve the real problem, but only make it worse. – BalusC Nov 14 '15 at 15:57
  • I read the post, so I understand I should not save the files in the WEB-CONTENT folder? Better to store them in my local drive? – Alex Reds Nov 19 '15 at 16:41

1 Answers1

-1

Add a slash at the end of your directory path

File imagesFolder = new File("/uploading images/WebContent/images/");

The File class isn't recognising the path as a directory, so it cannot get the files within.

Bryan Lopez
  • 1
  • 1
  • 1