简体   繁体   English

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

[英]How can I list all the files in folder on tomcat?

I have got a folder with many excel documents in it on tomcat and i want those files to be available when i got go the that folder's url in the browser (eg http;//localhost:8080/myfolder) 我在tomcat上有一个包含许多excel文件的文件夹,当我在浏览器中找到该文件夹​​的URL时,我希望这些文件可用(例如http; // localhost:8080 / myfolder)

at the moment when i try to access a folder i get a 404 error. 在我尝试访问文件夹时,我收到404错误。 by if i try to access a file that is in that folder, it works. 如果我尝试访问该文件夹中的文件,它的工作原理。

The DefaultServlet of Tomcat is by default configured to not show directory listings. 默认情况下,Tomcat的DefaultServlet配置为不显示目录列表。 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 您需要打开Tomcat自己的/conf/web.xml文件(查看Tomcat安装文件夹),搜索DefaultServlet<servlet>条目,然后更改其listings初始化参数

<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. 请记住,这会影响您的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. 如果你只想为一个单独的文件夹启用它,你必须自己编写一些Servlet代码,它在servlet端的java.io.File API的帮助下完成工作,收集文件和一些HTML / CSS在JSP方面以一种整洁的方式呈现它。

You can also enable it starting from a given url pattern. 您也可以从给定的url模式开始启用它。 Just add the servlet and servlet-mapping to you app web.xml 只需将servlet和servlet-mapping添加到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. 在此示例中,“/ ws-definitions /”下面的目录将被侦听。

Here's some documentation explaining how to do this. 这里有一些文档解释了如何执行此操作。

http://tomcat.apache.org/tomcat-7.0-doc/default-servlet.html 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. 其基本思路是,它的值更改listings参数,以true在主web.xml的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 http://tomcat.apache.org/tomcat-7.0-doc/default-servlet.html#dir

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. 如果您使用的是Tomcat 6(实现Servlet 2.5规范)或更新版本,则无需更改CATALINA_HOME / conf /目录中的web.xml以显示目录列表。 Instead you should change the web application's own web.xml file under WEB-INF. 相反,您应该在WEB-INF下更改Web应用程序自己的web.xml文件。

As Adarshr mentioned, this is what you need to add to the 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>

You also need to add the following 您还需要添加以下内容

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

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 删除它们应该完美

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

如果您只是尝试为servlet之外的文件实现基于Web的文件浏览器,则可以使用此答案中提到的自定义Web应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取文件夹内所有文件的列表 - how can i get the list of all files Inside a folder 如何在tomcat文件夹中写入文件? - How can I write a file in tomcat folder? 如何列出和选择设备中的所有音频文件? - how can i list and choose all audio files in the device? 您如何在Java中以递归方式列出与扩展名数组不匹配的文件夹中的所有文件? - How can you in Java list all files recursively in a folder that do not match an array of extensions? 如何在服务器(Tomcat 7)中创建文件夹? - How can I create a folder inside a server (Tomcat 7)? 如何使用 JGit 库获取 git 存储库文件夹中所有文件的 SHA 值列表 - How do I get the list of SHA values of all the files in a folder of my git repo using JGit library 我如何在Java驱动器中获取所有目录和文件列表。 (C:\\中的所有文件) - How can i get all the list of directories and files in a drive in java. (All files in C:\) 如何隐藏TOMCAT webapp文件夹中的JSP文件 - How to hide JSP files in TOMCAT webapp folder 如何将jar(或文件夹)中的所有.class文件一次更改为.java(或.txt)? - How can I change all .class files in a jar(or folder) to .java (or .txt) at once? 如何使用swing制作文件夹中所有文件的列表? - How to make List with all files in folder using swing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM