How to list contents of a server directory using JSP?

后端 未结 5 1601
心在旅途
心在旅途 2020-12-09 20:03

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)?

<
相关标签:
5条回答
  • 2020-12-09 20:07

    A correct/working example:

    File jsp = new File(request.getRealPath(request.getServletPath()));
    File dir = jsp.getParentFile();
    File[] list = dir.listFiles();
    
    0 讨论(0)
  • 2020-12-09 20:19

    you should know the path of the jsp within your web application so you can pass that to getRealPath()

    File jsp = request.getRealPath(pathToJspInWebapp);  //eg. /WEB-INF/jsp/my.jsp
    File directory = jsp.getParentFile();
    File[] list = directory.listFiles();
    
    0 讨论(0)
  • 2020-12-09 20:20

    As of Version 2.1 of the Java Servlet API use:

    File jsp = new File(request.getSession().getServletContext().getRealPath(request.getServletPath()));
    File dir = jsp.getParentFile();
    File[] list = dir.listFiles();
    
    0 讨论(0)
  • 2020-12-09 20:26
            <%@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

    0 讨论(0)
  • 2020-12-09 20:31

    I have used this one,

    File jspFile = new File(request.getRealPath(request.getServletPath()));
            File dir = jspFile.getParentFile();
            String requestURL = request.getRequestURL().toString();
            String urlDir = requestURL.substring(0, requestURL.lastIndexOf('/'));
    
            File[] files = dir.listFiles(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return name.endsWith(".ipa");
                }
            });
    
    0 讨论(0)
提交回复
热议问题