Java 如何使用 JSP 列出服务器目录的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/947730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:25:27  来源:igfitidea点击:

How to list contents of a server directory using JSP?

javajspfile

提问by ivan_ivanovich_ivanoff

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

编写 JSP 文件时,如何在运行时获取该文件的当前目录
(以便能够迭代该目录并列出其内容)?

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

是否会因为某些安全问题而限制某些文件 I/O 操作?

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.

编辑:
我不会问它是否像 一样简单new File("."),因为这只会给出服务器可执行文件的目录。

采纳答案by objects

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

您应该知道 web 应用程序中 jsp 的路径,以便您可以将其传递给 getRealPath()

File jsp = request.getRealPath(pathToJspInWebapp);  //eg. /WEB-INF/jsp/my.jsp
File directory = jsp.getParentFile();
File[] list = directory.listFiles();

回答by Pavel Vlasov

A correct/working example:

一个正确/有效的例子:

File jsp = new File(request.getRealPath(request.getServletPath()));
File dir = jsp.getParentFile();
File[] list = dir.listFiles();

回答by Max GIesbert

As of Version 2.1 of the Java Servlet API use:

从 Java Servlet API 的 2.1 版开始,使用:

File jsp = new File(request.getSession().getServletContext().getRealPath(request.getServletPath()));
File dir = jsp.getParentFile();
File[] list = dir.listFiles();

回答by Code Spy

        <%@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

“C:/mydirectory/”替换为您的目录

回答by karim

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");
            }
        });