0

so Im working on viewing files within A JSP page. However, any folders within a directory only appear as '.DS_Store' Rather than showing all folders with their correct names. I nhave used this code from this thread: How to list contents of a server directory using JSP?

The code:

<table class="main" id="tableMain">
<thead>
<th scope="col">First</th>
<%@page import="java.io.*" %> 
<%@page import="java.util.*" %> 

<%!        public void GetDirectory(String a_Path, Vector a_files, Vector a_folders) {
            File l_Directory = new File(a_Path);
            File[] l_files = l_Directory.listFiles();

            for (int c = 0; c < l_files.length; c++) {
                if (l_files[c].isDirectory()) {
                    a_folders.add(l_files[c].getName());
                } else {
                    a_files.add(l_files[c].getName());
                }
            }


        }
    %> 

    <%
        Vector l_Files = new Vector(), l_Folders = new Vector();
        GetDirectory("/Library/Tomcat/webapps/HelloServlet/uploads", l_Files, l_Folders);




        for (int a = 0; a < l_Files.size(); a++) {
            out.println("<tr>");
            out.println("<td>" + l_Files.elementAt(a).toString() + "</td>");
            out.println("</tr>");
        }

    %> 

Any help would be appreciated, thanks!

kieron
  • 332
  • 3
  • 19

2 Answers2

1

I suspect, you're viewing the contents of an empty directory? .DS_Store files are automatically generated on MacOS by finder. It's basically an index file that contains preview pictures and metadata for the current directory. They exist in a directory that you have opened with finder before, I believe.

In the case that you're only seeing suhc a file in a directory, I would suspect it is because the directory is empty except for that file.

For debugging purposes, you could also render the absolute path of files and directories shown (use .getAbsolutePath() instead of .getName()) That might give you a better indication on what is happening.

Matthias
  • 2,622
  • 1
  • 18
  • 29
0

This doesnt remove the .DS_Store directory, however it lists folders within the directory as well as files:

String file = application.getRealPath("/safe");
File f = new File(file);
String [] fileNames = f.list();
File [] fileObjects= f.listFiles();
for (int i = 0; i < fileObjects.length; i++) {
    if(!fileObjects[i].isFile() || !fileObjects[i].isDirectory()){
    String fname = file+fileNames[i];
    out.print("<tr>");
    out.println("<td>" + fileNames[i] + "</td>");
    out.print("</tr>");
kieron
  • 332
  • 3
  • 19