使用JSP列出服务器目录的内容
我在服务器上的以下目录中有文件:使用JSP列出服务器目录的内容,jsp,Jsp,我在服务器上的以下目录中有文件: D:\tomcat8\webapps\schema_files\ 我想列出上面目录中存在的所有文件 我试过的 <% String folder=application.getRealPath("D:/tomcat8/webapps/schema_files"); File file=new File(folder); String fileNames[]=file.list(); System.out.println("fileNames[] : "+f
D:\tomcat8\webapps\schema_files\
我想列出上面目录中存在的所有文件
我试过的
<%
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”中,那个么下面的代码就可以工作了
<%
String folder=application.getRealPath("/WEB-INF/filefolder");
File file=new File(folder);
String fileNames[]=file.list();
System.out.println("fileNames[] : "+fileNames[0]);
%>
请帮帮我……这是可行的,但可能有更好的方法可以显式地用于JSP
<%
File file=new File("D:\\tomcat8\\webapps\\schema_files");
String fileNames[]=file.list();
System.out.println("fileNames[] : "+fileNames[0]);
%>
您可以使用
application.getRealPath(字符串args0)
和application.getContextPath()
指向您的服务器目录,如下所示:
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]);
File File=new文件(application.getRealPath(application.getContextPath());
字符串文件名[]=file.list();
对于(int i=0;i
可以获得应用程序的根路径,因此它类似于localhost:port/yourappname
。然后,您只需将其分配给其他变量,如字符串路径,并添加子文件夹路径或其他内容
例如:
<%=request.getContextPath()%>/folder1/folder2/webpage.html
/folder1/folder2/webpage.html
您的程序现在做什么?我是说当前的output@Dashovsky输出为“System.out.println”(“fileNames[]:“+fileNames[0]);”行中的“java.lang.NullPointerException”;您的程序是否部署到tomcat服务器上的.war文件中?在何处传递目录路径?