如何使用JSP列出服务器目录的内容? [英] How to list contents of a server directory using JSP?

查看:86
本文介绍了如何使用JSP列出服务器目录的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

由于某些安全问题,是否会限制某些文件I / O操作?

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

我更喜欢没有访问某些特定于实现的
服务器变量/属性的解决方案。

编辑:

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


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

推荐答案

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

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();

这篇关于如何使用JSP列出服务器目录的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆