Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

java - How to list contents of a server directory using JSP?

When writing a JSP file, how can I get the current directory of this file at runtime
(to be able to iterate the directory and list its contents)?

Would some file I/O operations be restricted because of some security issues?

I would prefer a solution without accessing some implementation-specific server variables / properties.

EDIT:
I wouldn't ask if it were as simple as new File("."), because this would just give the directory of server's executables.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
        <%@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("C:/mydirectory/", l_Files, l_Folders);

            //folders should be left out... 
            //for( int a = 0 ; a<l_Folders.size() ; a++ ) 
            //out.println( "[<b>"+l_Folders.elementAt(a).toString() + "</b>]<br>") ; 

            //generate files as XML 
            out.println("<music>");

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

            out.println("</music>");
        %> 

Replace "C:/mydirectory/" with your directory


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...