如何列出tomcat文件夹中的所有文件?

发布于 2024-11-29 19:41:27 字数 155 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

爱*していゐ 2024-12-06 19:41:27

Tomcat 的 DefaultServlet 默认配置为不显示目录列表。需要打开Tomcat自带的/conf/web.xml文件(在Tomcat安装文件夹中查找),搜索DefaultServlet条目code>,然后将其 listings 初始化参数从 更改

<init-param>
    <param-name>listings</param-name>
    <param-value>false</param-value>
</init-param>

<init-param>
    <param-name>listings</param-name>
    <param-value>true</param-value>
</init-param>

请记住,这会影响您的 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 the DefaultServlet and then change its listings initialization parameter from

<init-param>
    <param-name>listings</param-name>
    <param-value>false</param-value>
</init-param>

to

<init-param>
    <param-name>listings</param-name>
    <param-value>true</param-value>
</init-param>

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 the java.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.

慕巷 2024-12-06 19:41:27

您还可以从给定的 url 模式开始启用它。
只需将 servlet 和 servlet-mapping 添加到您的应用程序 web.xml 中,

<servlet>
    <!-- List files in /ws-definitions -->
    <servlet-name>ListWsDefinitions</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <init-param>
        <param-name>listings</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>100</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>ListWsDefinitions</servlet-name>
    <url-pattern>/ws-definitions/*</url-pattern>
</servlet-mapping>

在此示例中,“/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

<servlet>
    <!-- List files in /ws-definitions -->
    <servlet-name>ListWsDefinitions</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <init-param>
        <param-name>listings</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>100</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>ListWsDefinitions</servlet-name>
    <url-pattern>/ws-definitions/*</url-pattern>
</servlet-mapping>

In this example directories below "/ws-definitions/" will be listen.

醉态萌生 2024-12-06 19:41:27

这里有一些文档解释了如何执行此操作。

http://tomcat.apache.org/tomcat-7.0-doc/default -servlet.html

基本思路是在tomcat的主web.xml中将listings参数的值改为true

<servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>
      org.apache.catalina.servlets.DefaultServlet
    </servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <init-param>
        <param-name>listings</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

但上面会暴露所有目录。为了进行精细控制,请按照此处说明的步骤操作:

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 to true in the main web.xml of tomcat.

<servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>
      org.apache.catalina.servlets.DefaultServlet
    </servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <init-param>
        <param-name>listings</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

梦一生花开无言 2024-12-06 19:41:27

如果您使用的是 Tomcat 6(实现 Servlet 2.5 规范)或更新版本,则不必更改 CATALINA_HOME/conf/ 目录中的 web.xml 来显示目录列表。相反,您应该更改 WEB-INF 下 Web 应用程序自己的 web.xml 文件。

正如 Adarshr 提到的,这是您需要添加到 web.xml 中的内容

<servlet>
  <servlet—name>default</servlet—name>
  <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
  <init-param>
    <param-name>debug</param-name>
    <param-value>0</param-value>
  </init-param>
  <init-param>
    <param-name>listings</param-name>
    <param-value>true</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>

您还需要添加以下内容

<servlet-mapping>
   <servlet-name>default</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>

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

<servlet>
  <servlet—name>default</servlet—name>
  <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
  <init-param>
    <param-name>debug</param-name>
    <param-value>0</param-value>
  </init-param>
  <init-param>
    <param-name>listings</param-name>
    <param-value>true</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>

You also need to add the following

<servlet-mapping>
   <servlet-name>default</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>
紫竹語嫣☆ 2024-12-06 19:41:27

这是一个简单的servlet,可能是完全定制方法的开始。

Here's a simple servlet that might be a start for a completely custom approach.

吾性傲以野 2024-12-06 19:41:27

如果更改列表参数值不起作用,请尝试编辑欢迎文件列表

默认值如下:

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

按如下方式编辑:

<welcome-file-list>
    <welcome-file></welcome-file>
    <welcome-file></welcome-file>
    <welcome-file></welcome-file>
</welcome-file-list>

删除它们后它应该可以完美工作

If changing the listings param value doesn't work, try editing the welcome file list

default values were the following:

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

edit it as follows:

<welcome-file-list>
    <welcome-file></welcome-file>
    <welcome-file></welcome-file>
    <welcome-file></welcome-file>
</welcome-file-list>

on removing them it should work perfectly

白云不回头 2024-12-06 19:41:27

如果您只是尝试为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文