本文参考:
- https://blog.csdn.net/ypppk/article/details/117994975
- https://blog.csdn.net/weixin_34195546/article/details/89659425
1. 设置tomcat为文件服务器
修改server.xml文件
<!--在Host里面设置>
<Host name="localhost" appBase=""
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<!--设置可以访问权限-->
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="10.10.*|192.168.*.*" deny=""/>
<!-- ***********自定义tomcat访问路径*************
1. docBase真实目录路径
2. path 虚拟访问路径
-->
<Context docBase="/appserver/backup" reloadable="true" path="/sql" />
</Host>
2. tomcat显示文件列表目录
修改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>
<!--listings中的param-value设置为true,允许列出目录中的文件-->
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
3. 定制美化目录
3.1 在步骤2中的servlet添加init-param
<init-param>
<param-name>globalXsltFile</param-name>
<param-value>global.xslt</param-value>
</init-param>
3.2 创建global.xslt文件
touch global.xslt
global.xslt 是官方的示例 ,这里对其进行了样式调整和排序设置
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:output method="html" html-version="5.0"
encoding="UTF-8" indent="no"
doctype-system="about:legacy-compat"/>
<xsl:template match="listing">
<html>
<head>
<title>
Sample Directory Listing For
<xsl:value-of select="@directory"/>
</title>
<style>
h1 {color : white;background-color : #0086b2;}
h3 {color : white;background-color : #0086b2;}
body {font-family : sans-serif,Arial,Tahoma;
color : black;background-color : white;}
b {color : white;background-color : #0086b2;}
a {color : black;} HR{color : #0086b2;}
<!--设置文件列表显示文件的间距 -->
tbody { line-height: 0.3em; }
</style>
</head>
<body>
<h1>Sample Directory Listing For
<xsl:value-of select="@directory"/>
</h1>
<hr style="height: 1px;" />
<table style="width: 100%;">
<tr>
<th style="text-align: left;">Filename</th>
<th style="text-align: center;">Size</th>
<th style="text-align: right;">Last Modified</th>
</tr>
<xsl:apply-templates select="entries"/>
</table>
<xsl:apply-templates select="readme"/>
<hr style="height: 1px;" />
<h3>Apache Tomcat/<version-major-minor/></h3>
</body>
</html>
</xsl:template>
<xsl:template match="entries">
<xsl:apply-templates select="entry">
<!-- 添加关键排序功能 -->
<xsl:sort select="@urlPath" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="readme">
<hr style="height: 1px;" />
<pre><xsl:apply-templates/></pre>
</xsl:template>
<xsl:template match="entry">
<tr>
<td style="text-align: left;">
<xsl:variable name="urlPath" select="@urlPath"/>
<a href="{$urlPath}">
<pre><xsl:apply-templates/></pre>
</a>
</td>
<td style="text-align: right;">
<pre><xsl:value-of select="@size"/></pre>
</td>
<td style="text-align: right;">
<pre><xsl:value-of select="@date"/></pre>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
更多请参考官网......