29

我有一个目录,一个进程将一些.pdf文件上传到该目录。这个过程是我无法控制的。

我需要使用 Tomcat 通过网站提供这些文件。

我有一个/var/lib/tomcat5/webapps/test1可用于网络的目录,我可以使用浏览器查看其中的文件。

因此,我创建了一个指向包含.pdffiles: 的目录的符号链接/var/lib/tomcat5/webapps/test1/files/,但我在该目录中看不到任何内容。

如何test1仅在目录中启用符号链接?我不想在任何地方启用符号链接,只是为了让包含.pdf文件的目录对网络可用。

4

7 回答 7

53

META-INF/context.xml创建包含的解决方案存在一些问题<Context path="/myapp" allowLinking="true">

最大的问题是,如果 aconf/context.xml存在,则allowLinkingin the <Context>there优先于a <Context>in a META-INF/context.xml。而如果 in 中的conf/context.xml没有明确定义allowLinking,那就同说allowLinking="false"。(请参阅对上下文优先级问题的回答)

为确保您的应用允许链接,您必须说<Context override="true" allowLinking="true" ...>.

另一个问题path="/myapp"META-INF/context.xml. 为防止混淆,最好将其排除在外。path在 a中唯一<Context>有任何影响的时间是在 中server.xml,并且官方 Tomcat 文档建议不要<Context>s 放在 a 中server.xml

最后,myapp/META-INF/context.xml我建议使用文件而不是conf/Catalina/localhost/myapp.xml文件。这种技术意味着你可以保持你的内容META-INF干净,这是你的 webapp 的胆量——我不喜欢冒险在我的 webapp 的胆量里乱七八糟。:-)

于 2008-11-27T01:32:32.237 回答
16

META-INF在 Web 应用程序的目录中创建一个 context.xml 文件,其中包含:

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

<Context path="/myapp" allowLinking="true">

</Context>

更多信息:http ://www.isocra.com/2008/01/following-symbolic-links-in-tomcat/

于 2008-11-24T19:21:44.343 回答
8

这在 Tomcat 8+ 中的工作方式不同

http://tomcat.apache.org/migration-8.html

<Resources allowLinking="true" />
于 2018-02-24T12:15:03.560 回答
7

是的,我知道这是一个老问题,但我找到了一个新的解决方案,使用 mount 和 --bind 选项而不是符号链接,并且 tomcat 不需要任何重新配置​​:

cd /var/lib/tomcat5/webapps/test1/

mkdir 文件

mount --bind /path/to/actual/upload/directory/files 文件

于 2015-06-08T14:50:58.453 回答
4

Context可以存在的地方有4个。

  1. tomcatdir/conf/server.xml
  2. tomcatdir/conf/context.xml
  3. tomcatdir/conf/Catalina/localhost/appname.xml
  4. tomcatdir/webapps/appname/META-INF/context.xml

在 tomcat 8 的情况下,allowlinking 属性不应在 Context 中指定,而应在 Resources 标签中指定。我的 tomcatdir/conf/context.xml 看起来像这样

<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
 <Resources allowLinking="true" cachingAllowed="true" cacheMaxSize="100000" />
</Context>

这个解决方案现在对我来说很好。但我也想分享我在提出这个解决方案之前犯的错误。

我在 tomcatdir/conf/server.xml 和 tomcatdir/conf/context.xml 中都定义了资源。并且仅在 tomcatdir/conf/server.xml 中设置了 allowLinking="true"。

我发现如果您不指定allowLinking,则等于将其设置为false。所以我从 server.xml 中删除了 Resources 标签,只留下了 tomcatdir/conf/context.xml,其中包含 allowLinking="true" 属性。

于 2017-04-28T06:30:15.153 回答
1

我以另一种方式做到了。我编辑这个其他配置文件:apache-tomcat-7.0.33/conf/ server.xml 在我添加的主机标签中:

<Context path="/data" docBase="C:\datos" debug="0" reloadable="true" crossContext="false"/>

因此,您可以通过以下方式访问:http://localhost/data

于 2015-05-21T08:00:09.670 回答
0

将以下行添加到 conf/context.xml 为我在 apache tomcat 8.5+ 上启用软链接

<资源allowLinking="true" cachingAllowed="true" cacheMaxSize="100000" >

于 2018-11-27T17:34:46.280 回答