【Tomcat】tomcat设置http文件下载,配置文件下载目录

  tomcat作为http的下载服务器,网上有很多办法

但我认为最简单的是:(亲测有效)

  1、直接把文件放在 /var/lib/tomcat6/webapps/ROOT 目录下,

  2、然后在网址中访问: http://192.168.2.31:8080/download.zip 便可下载。

但是,如何你不想放在webapps/ROOT下,就要对想要下载的目录进行配置:(亲测有效)

1、在tomcat 安装目录confCatalinalocalhost下建立任意文件名xml文件,比如:download.xml,

内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="fileDown" reloadable="true" docBase="/opt/downLoadFile" crossContext="true">
</Context>

   context元素有一个crossContext属性,如果配置为true,则可以实现在同一个tomcat下的多个web应用之间实现ServletContext对象访问。该属性主要用于跨应用访问数据,官网解释:

Set to true if you want calls within this application to ServletContext.getContext() to successfully return a request dispatcher for other web applications running on this virtual host. Set to false (the default) in security conscious environments, to make getContext() always return null.

2、配置web.xml(tomcat的配置文件),修改如下配置(大约在105行左右),主要是为了在界面中列出目录结构,可以看到目录的结构

<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>

要将红色的false改为true。

  在 web 开发阶段,通常希望列出 web 路径下的所有页面,这样能方便选择需要调试的 jsp 页面,listings设置为true时表示列出页面,为false则表示不支持目录结构的显示

3、重启tomcat。

4、访问http://qiaoliqiang.cn/fileDown/2.txt

原文地址:https://www.cnblogs.com/qlqwjy/p/8858568.html