[Tomcat]软链接导致context.getRealPath取到null或者获取文件FileNotFoundException的解决方案

2021-09-13 19点热度 0人点赞 0条评论

问题描述

部署web的时候,为了方便期间,把用户资源做一个link附加到应用中(ln -s /storage /opt/tomcat/webapps/ROOT/xxx),结果发现tomcat里的程序无法读取到这个目录或者里面的文件

例如:ServletActionContext.getServletContext().getRealPath('/storage')就输出为null,其他文件操作的地方,报java.io.FileNotFoundException.

原因是Tomcat下,默认是不支持linking的方式的,需要在配置文件中开启linking

Tomcat6以及之前的版本

修改tomcat/conf/context.xml,在context里面增加:<Resources allowLinking="true" />

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context allowLinking="true">

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

</Context>

 

Tomcat7/Tomcat8以及以上版本

修改tomcat/conf/context.xml,在context下增加:<Resources allowLinking="true" />

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
    <!--增加allowLinking-->
    <Resources allowLinking="true" />
</Context>

修改后,重启tomcat,就正常了。

admin

这个人很懒,什么都没留下

文章评论

*

code