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

笑着哭i 提交于 2019-11-27 04:19:24

问题


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)

at the moment when i try to access a folder i get a 404 error. by if i try to access a file that is in that folder, it works.


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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>



回答5:


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




回答6:


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




回答7:


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.



来源:https://stackoverflow.com/questions/7068046/how-can-i-list-all-the-files-in-folder-on-tomcat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!