简体   繁体   English

Tomcat中的Java文件路径

[英]Java filepath in tomcat

I have a java application running on a tomcat server. 我有一个在Tomcat服务器上运行的Java应用程序。 The application needs to read/write xml files to a directory. 应用程序需要将xml文件读/写到目录中。

I programmed it so that the files are written and read from the same directory using constant 我对其进行了编程,以便使用常量从同一目录写入和读取文件

public static final String DICTIONARY_PATH = getRelativePath() + "/res/dictionaries/";

which calls this method 调用此方法

public static String getRelativePath() {
    return new File("").getAbsolutePath();
}

The constant is used in the methods readFromFile(String language) and writeToFile(Dictionary dictionary) 该常量在方法readFromFile(String language)writeToFile(Dictionary dictionary)

When calling these methods from the main method running in IntelliJ (for testing purposes) it works beautifully and reads and writes files to the specified folder. 从IntelliJ中运行的主要方法(出于测试目的)调用这些方法时,它可以正常工作,并且可以将文件读写到指定的文件夹中。

When these methods are called from the application when running on the tomcat server it can't find the files because it's looking in the tomcat/bin folder of tomcat while the files are located in tomcat/webapps/application/WEB-INF/classes/dictionaries 当在tomcat服务器上运行时从应用程序调用这些方法时,它找不到文件,因为当文件位于tomcat/webapps/application/WEB-INF/classes/dictionaries它正在tomcat/bin文件夹中查找tomcat/webapps/application/WEB-INF/classes/dictionaries

How can I implement the method getRelativePath() so that it works on both the tomcat server as locally in IntelliJ (by calling the main method) 我如何实现方法getRelativePath()以便它可以在IntelliJ中的本地Tomcat服务器上工作(通过调用main方法)

You can use a properties file in Java to denote where tomcat/webapps/application/WEB-INF/classes/dictionaries is. 您可以使用Java中的属性文件来指示tomcat/webapps/application/WEB-INF/classes/dictionaries在哪里。 This will help you to setup tomcat directory as your "Actual target". 这将帮助您将tomcat目录设置为“实际目标”。 using that property, you can always using relative paths eg ..\\..\\..\\C:\\myprojects\\ to go to your java project for testing. 使用该属性,您可以始终使用相对路径,例如..\\..\\..\\C:\\myprojects\\进入Java项目进行测试。 Also, use them in combination with env variable. 另外,将它们与env变量结合使用。 Note that you can always setup environment variables and access them using System.getenv(String name) . 请注意,您始终可以设置环境变量并使用System.getenv(String name)

In case you don't know about properties file setup (very basic one) - http://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/ 如果您不了解属性文件的设置(非常基础) -http://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/

The benefit of doing things through prop file is that your classes are intact. 通过prop文件处理事务的好处是您的类是完整的。 You change only properties file to point to values of whatever. 您仅更改属性文件以指向任何值。 Hardcoding file names and configs in OO design should be avoided if possible. 如果可能,应避免在OO设计中硬编码文件名和配置。

I believe better to use context params using a listener class. 我相信最好通过侦听器类使用上下文参数。 Simply store that path in context or in servlet context for that matter. 只需将该路径存储在上下文或servlet上下文中即可。 Or get relative path using servlet context. 或者使用servlet上下文获取相对路径。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM