tomcat配置外部静态资源映射路径

一、背景

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>

一切正常,可以如预期地在线收听和下载。

posted @ 2017-07-20 20:01  一沙世界  阅读(23224)  评论(0编辑  收藏  举报