简体   繁体   English

从不同Tomcat上的Tomcat Web应用程序读取文件

[英]Read a file from Tomcat webapps on different Tomcats

Consider the following: I got a csv-file. 考虑以下几点:我得到了一个csv文件。 This file is stored in the tomcat wepapps directory. 该文件存储在tomcat wepapps目录中。 The path to this directory is dependent of the computer used. 该目录的路径取决于所使用的计算机。 There are users that use different locations for their Tomcats and use different names. 有些用户为其Tomcat使用不同的位置并使用不同的名称。 I want to achieve the following. 我要实现以下目标。

Via Spring i did the following: I got an action which imports some data from the csv to the database. 通过Spring,我执行了以下操作:我执行了一个操作,该操作将一些数据从csv导入数据库。

<bean id="setCsvPathAction" class="custom.action.importcsv.SetCsvPathAction">
   <property name="csvPath" value="C:/temp/test.csv"/>
</bean>

This works fine for me. 这对我来说很好。 To make this path more configurable for others i got a system.properties file. 为了使该路径对其他人更可配置,我得到了一个system.properties文件。 With this file you can easily overrite properties: 使用此文件,您可以轻松地覆盖属性:

setCsvPathAction.csvPath=#{tomcat_home}/webapps/test.csv

That is what i want. 那就是我想要的。 I want the path to be on the Tomcat webapps directory. 我希望该路径位于Tomcat webapps目录中。 The above doesn't work. 上面的方法不起作用。 Do you have any hint how to achieve this? 你有什么提示如何实现这一目标吗?

Most people who use tomcat would have set CATALINA_HOME environment variable. 大多数使用tomcat的人都会设置CATALINA_HOME环境变量。 You can query this environment variable and retrieve value. 您可以查询此环境变量并检索值。

String tomcat_home = System.getenv("CATALINA_HOME");
if(tomcat_home){
    setCsvPathAction.csvPath=tomcat_home+"/webapps/test.csv"
}

Note: There's no guarantee that this environment variable is set on every system using tomcat 注意:不能保证在每个使用tomcat的系统上都设置此环境变量

More on getenv() - http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getenv%28%29 有关getenv()的更多信息-http: //docs.oracle.com/javase/7/docs/api/java/lang/System.html#getenv%28%29

If it is going to be in the web apps folder you could also just use the class loader to get the local path so you do not have to configure anything. 如果要放在web apps文件夹中,则也可以使用类加载器来获取本地路径,因此您无需进行任何配置。

someObject.getClass().getClassLoader().getResourceAsStream("path/refferenced/to/app/root/"); someObject.getClass()。getClassLoader()。getResourceAsStream(“ path / refferenced / to / app / root /”);

The solution for the .properties file was: ${catalina.home} Next i need a FileInputStream independent of the system environment. .properties文件的解决方案是:$ {catalina.home}接下来,我需要一个独立于系统环境的FileInputStream。 I tried the following: 我尝试了以下方法:

public class environmentTest {

    public static void main(final String[] args) {
        System.out.println(System.getenv("CATALINA_BASE"));
    }

}

null gets printed on the console. null在控制台上打印。

The class Person contains the name of the jpg file. Person类包含jpg文件的名称。 This file is located at the webapps folder. 该文件位于webapps文件夹中。 I want the path to be independent of the computer used. 我希望路径独立于所使用的计算机。 I tried the following: 我尝试了以下方法:

File file = new File(System.getenv("CATALINA_HOME") + "/webapps/" + person.getPhoto());
FileInputStream fileInputStream = new FileInputStream(file);

Sadly this doesn't work because, as unekwu said, this environment variable is not set on all systems. 遗憾的是,这是行不通的,因为正如unekwu所说,此环境变量未在所有系统上设置。 Do you have any idea what i can do in this situation? 您知道在这种情况下我能做什么吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从已部署的Web应用程序获取Tomcats webapps目录中文件的相对路径 - Relative path for getting file in Tomcats webapps directory from a deployed web application 来自不同tomcat Webapp的Spring Bean发生冲突 - Conflicting spring beans from different tomcat webapps 从tomcat webapps文件夹中读取属性文件 - Reading Property file from tomcat webapps folder 为不同的tomcat Webapp分配2 ips - Assign 2 ips to different tomcat webapps 当2个tomcat位于不同机器中时,如何在tomcat中实现负载均衡? - How to implement load balancer in tomcat when 2 tomcats are in different machines? 将war文件部署到webapps后,Java项目未在tomcat管理器的tomcat服务器上运行? - Java project is not running on tomcat server from tomcat manager after deploying war file to webapps? Jar 文件无法在 tomcat webapps 中加载 - Jar file failed to load in tomcat webapps JSP文件在Eclipse中可以正常工作,但在Tomcat / webapps中却不能 - JSP file working fine in Eclipse but not in Tomcat / webapps Tomcat 7 ClassLoader如何从2个Web应用程序,相同的包/类层次结构,不同的类加载? - Tomcat 7 How ClassLoader loads from 2 webapps, same package/classes hierarchy, different classes? 如何将文件从Java servlet上传到Tomcat / webapps中Web服务器上的某个位置 - How do I upload a file from a Java servlet to a location on a web server within Tomcat/webapps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM