使用JSP列出服务器目录的内容 [英] list contents of a server directory using JSP
本文介绍了使用JSP列出服务器目录的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的服务器上的文件位于以下目录中:
I have files on server in following directory:
D:\tomcat8\webapps\schema_files\
我想列出以上目录中的所有文件.
I want to list all files present in above directory.
我尝试过的
<%
String folder=application.getRealPath("D:/tomcat8/webapps/schema_files");
File file=new File(folder);
String fileNames[]=file.list();
System.out.println("fileNames[] : "+fileNames[0]);
%>
它不起作用.
但是,如果我将文件存储在应用程序本身的"/WEB-INF/filefolder"中,则以下代码有效.
However if I store files in application itself at "/WEB-INF/filefolder" then following code works.
<%
String folder=application.getRealPath("/WEB-INF/filefolder");
File file=new File(folder);
String fileNames[]=file.list();
System.out.println("fileNames[] : "+fileNames[0]);
%>
请帮助我.
推荐答案
您可以使用application.getRealPath(String args0)
和application.getContextPath()
指向服务器目录,如下所示:
You can use application.getRealPath(String args0)
and application.getContextPath()
pointing to your server directory like this:
File file=new File(application.getRealPath(application.getContextPath()));
String fileNames[]=file.list();
for(int i=0;i<fileNames.length;i++)
System.out.println(fileNames[i]);
application
是JSP中可用的隐式对象,就像session
和request
一样.
application
is an implicit object available in JSP, just like session
and request
.
这篇关于使用JSP列出服务器目录的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文