1. 程式人生 > >使用tomcat配置檔案下載伺服器,自定義下載列表

使用tomcat配置檔案下載伺服器,自定義下載列表

http://www.cnblogs.com/huxdiy/p/tomcat%E6%96%87%E4%BB%B6%E4%B8%8B%E8%BD%BD.html

先上圖,利用tomcat,這個下載介面沒有程式碼,點選檔名即可下載

詳細參考:http://tomcat.apache.org/tomcat-7.0-doc/default-servlet.html

 tomcat 版本:7.0.42 /6.0.26/7.0.23已測試通過

下面是配置步驟

1  建立被下載檔案目錄(以D://download為例,下載檔案在download目錄下)

2、配置虛擬目錄

         在tomcat 安裝目錄\conf\Catalina\localhost下建立任意檔名xml檔案(缺失資料夾需要自己建立)

         示例:download.xml

<?xml version="1.0" encoding="UTF-8"?>

<Context  reloadable="true" docBase="D://download" crossContext="true">

</Context>

--配置檔名為訪問下載目錄 即訪問地址為:http://ip:8080/download

3、配置web.xml(tomcat的配置檔案),新增如下配置

複製程式碼
   <init-param>

            <param-name>listings</
param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>contextXsltFile</param-name> <param-value>/load.xsl</param-value> </init-param>
複製程式碼

-- listings預設為false  ,修改為true

-- contextXsltFile為啟用自定義下載檔案列表引數 value值為定義規範,一個.xsl檔案

4、新增輸出規範,示例:load.xml,配置到資料夾D://download下

複製程式碼
<?xml version="1.0"?>

 

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

  version="1.0">

 

  <xsl:output method="html" encoding="UTF-8" indent="no"/>

 

  <xsl:template match="listing">

   <html>

    <head>

      <title>

      Download

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

      </style>

    </head>

    <body>

      <h1>Download

            <xsl:value-of select="@directory"/>

      </h1>

      <hr size="1" />

      <table cellspacing="0"

                  width="100%"

            cellpadding="5"

                  align="center">

        <tr>

          <th align="left">Filename</th>

          <th align="center">Size</th>

          <th align="right">Last Modified</th>

        </tr>

        <xsl:apply-templates select="entries"/>

        </table>

      <xsl:apply-templates select="readme"/>

      <hr size="1" />

      <h3>********* 2013 </h3>

    </body>

   </html>

  </xsl:template>

 

 

  <xsl:template match="entries">

    <xsl:apply-templates select="entry"/>

  </xsl:template>

 

  <xsl:template match="readme">

    <hr size="1" />

    <pre><xsl:apply-templates/></pre>

  </xsl:template>

 

  <xsl:template match="entry">

    <tr>

      <td align="left">

        <xsl:variable name="urlPath" select="@urlPath"/>

        <a href="{$urlPath}">

          <tt><xsl:apply-templates/></tt>

        </a>

      </td>

      <td align="right">

        <tt><xsl:value-of select="@size"/></tt>

      </td>

      <td align="right">

        <tt><xsl:value-of select="@date"/></tt>

      </td>

    </tr>

  </xsl:template>

 

</xsl:stylesheet>
複製程式碼

5、重啟tomcat      訪問http://ip:8080/download,即可隨意下載配置目錄下的檔案

轉載請註明出處!!!