1. 程式人生 > >tomcat配置外部靜態資源映射路徑

tomcat配置外部靜態資源映射路徑

docs usr 頁面 ext -- tween sign apps 測試

一、背景

1.有一個錄音軟件每天生成很多新的錄音文件。

2.現在想通過一個WEB項目頁面下載這些錄音文件。

3.很顯然這些錄音文件放在WEB項目下不是很合適(WEB項目更新是個大麻煩,海量的錄音文件要處理)。

二、外部靜態資源映射

首先想到的就是tomcat能否直接配置靜態資源路徑?哈哈,答案是yes!

只需要修改tomcat的配置文件server.xml就能滿足我的需求。

1. 文件路徑

你的tomcat的安裝路徑/apache-tomcat-你的版本/conf

例如:

/usr/local/apache-tomcat-7.0.79/conf/server.xml

2. 修改如下

<Host name="
localhost" appBase="webapps" 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 &quot;%r&quot; %s %b" />   

  <!-- 增加的靜態資源映射配置 -->   <Context path="/RecordFile" docBase="/home/lings/recordings" reloadable="true" crossContext="true"></Context>

</Host>

紅色那一行就是增加的靜態資源映射配置。

三、測試

1. 實際錄音文件的地址為

/home/lings/recordings/test.wav

2. 頁面中資源引用方式為

<div>
    <audio src="/RecordFile/test.wav" controls="controls"></audio>
</div>

一切正常,可以如預期地在線收聽和下載。

tomcat配置外部靜態資源映射路徑