如何列出tomcat文件夹中的所有文件?
我在 Tomcat 上有一个包含许多 Excel 文档的文件夹,我希望当我在浏览器中访问该文件夹的 URL(例如 http://localhost:8080/myfolder)时这些文件可用。
当我尝试访问文件夹时,出现 404 错误。如果我尝试访问该文件夹中的文件,它就会起作用。
I have got a folder with many Excel documents in it on Tomcat and I want those files to be available when I go to that folder's url in the browser (eg http://localhost:8080/myfolder).
At the moment when I try to access a folder i get a 404 error. If I try to access a file that is in that folder, it works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Tomcat 的
DefaultServlet
默认配置为不显示目录列表。需要打开Tomcat自带的/conf/web.xml
文件(在Tomcat安装文件夹中查找),搜索DefaultServlet
的
条目code>,然后将其listings
初始化参数从 更改为
请记住,这会影响您的 web 应用程序的所有 文件夹。如果您只想为单个文件夹启用此功能,则必须自己编写一些
Servlet
代码,在java.io.File
API 的帮助下完成这项工作在 servlet 端收集文件,并在 JSP 端收集一些 HTML/CSS,以简洁的方式呈现它。The
DefaultServlet
of Tomcat is by default configured to not show directory listings. You need to open Tomcat's own/conf/web.xml
file (look in Tomcat installation folder), search the<servlet>
entry of theDefaultServlet
and then change itslistings
initialization parameter fromto
Keep in mind that this affects all folders of your webapp. If you want to enable this for only an individual folder, you've got to write some
Servlet
code yourself which does the job with help of thejava.io.File
API in servlet side to collect the files and some bunch of HTML/CSS in JSP side to present it in a neat fashion.您还可以从给定的 url 模式开始启用它。
只需将 servlet 和 servlet-mapping 添加到您的应用程序 web.xml 中,
在此示例中,“/ws-definitions/”下面的目录将被监听。
You can also enable it starting from a given url pattern.
Just add the servlet and servlet-mapping to you app web.xml
In this example directories below "/ws-definitions/" will be listen.
这里有一些文档解释了如何执行此操作。
http://tomcat.apache.org/tomcat-7.0-doc/default -servlet.html
基本思路是在tomcat的主
web.xml
中将listings
参数的值改为true
。但上面会暴露所有目录。为了进行精细控制,请按照此处说明的步骤操作:
http: //tomcat.apache.org/tomcat-7.0-doc/default-servlet.html#dir
Here's some documentation explaining how to do this.
http://tomcat.apache.org/tomcat-7.0-doc/default-servlet.html
The basic idea is to change the value of
listings
parameter totrue
in the mainweb.xml
of tomcat.But the above will expose all directories. In order to have fine control, follow the steps explained here:
http://tomcat.apache.org/tomcat-7.0-doc/default-servlet.html#dir
如果您使用的是 Tomcat 6(实现 Servlet 2.5 规范)或更新版本,则不必更改 CATALINA_HOME/conf/ 目录中的 web.xml 来显示目录列表。相反,您应该更改 WEB-INF 下 Web 应用程序自己的 web.xml 文件。
正如 Adarshr 提到的,这是您需要添加到 web.xml 中的内容
您还需要添加以下内容
If you are using Tomcat 6 (which implements Servlet 2.5 specification) or a newer version, you don't have to change the web.xml in the CATALINA_HOME/conf/ directory to display the directory listings. Instead you should change the web application's own web.xml file under WEB-INF.
As Adarshr mentioned, this is what you need to add to the web.xml
You also need to add the following
这是一个简单的servlet,可能是完全定制方法的开始。
Here's a simple servlet that might be a start for a completely custom approach.
如果更改列表参数值不起作用,请尝试编辑欢迎文件列表
默认值如下:
按如下方式编辑:
删除它们后它应该可以完美工作
If changing the listings param value doesn't work, try editing the welcome file list
default values were the following:
edit it as follows:
on removing them it should work perfectly
如果您只是尝试为 servlet 之外的文件实现基于 Web 的文件浏览器,则可以使用 此答案中提到的自定义 Web 应用程序。
If you are just trying to implement a web-based file browser for files outside of your servlet, you could use the custom webapp mentioned in this answer.