如何使用Java从linux环境中获取tomcat中当前目录的相对路径 [英] how to get relative path of current directory in tomcat from linux environment using java

查看:37
本文介绍了如何使用Java从linux环境中获取tomcat中当前目录的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用来从Web应用程序的外部读取属性文件.我在Windows环境中的tomcat中部署了war文件,并且能够使用以下代码从Web应用程序外部读取属性文件.

I would like to use to read properties file from out side of the my web application. I was deployed a war file in tomcat in windows environment and I am able to read properties file from outside of my web application using following code.

//(Method 1.)
String filePath = 
new java.io.File( ".").getCanonicalPath()+"/webapps/strsproperties/strs.properties";
// or  
//(Method 2.)
String filePath = new File(System.getProperty("catalina.base"))+ "/webapps/strsproperties/strs.properties";

InputStream inputStream = null;
inputStream = new FileInputStream(filePath);
properties.load(inputStream);
String application = properties.getProperty("applications");

在以上两种情况下,我都可以在Windows中读取filePath.

In above both case's I am able to read filePath in windows.

问题是我无法使用第一种方法(相对路径)过程在Linux中读取filePath.

Problem is I am not able to read filePath in Linux using 1st method ( relative path) process.

有什么方法可以使用Linux env中的tomcat中的相对路径读取文件?

Is there any way to read file using relative path in tomcat from Linux env ?.

推荐答案

您的问题是您不知道当前"目录是什么.如果您在Linux上将Tomcat作为服务启动,则当前目录可以是任何目录.因此, new File(.")将使您在文件系统中占据一个随机位置.

Your problem is that you don't know what the "current" directory is. If you start Tomcat on Linux as a service, the current directory could be anything. So new File(".") will get you a random place in the file system.

使用系统属性 catalina.base 更好,因为Tomcat的启动脚本 catalina.sh 将设置此属性.因此,只要您不尝试在其他服务器上运行应用程序,此方法就可以正常工作.

Using the system property catalina.base is much better because Tomcat's start script catalina.sh will set this property. So this will work as long as you don't try to run your app on a different server.

好的代码如下:

File catalinaBase = new File( System.getProperty( "catalina.base" ) ).getAbsoluteFile();
File propertyFile = new File( catalinaBase, "webapps/strsproperties/strs.properties" );

InputStream inputStream = new FileInputStream( propertyFile );

如您所见,代码不会混合字符串和文件.文件是文件,字符串只是文本.避免使用文本作为代码.

As you can see, the code doesn't mix strings and files. A file is a file and a string is just text. Avoid using text for code.

接下来,我正在使用 getAbsoluteFile()来确保在异常中得到有用的路径.

Next, I'm using getAbsoluteFile() to make sure I get a useful path in exceptions.

还要确保您的代码不会吞噬异常.如果您可以在代码中看到错误消息,则可以立即看到该代码试图在错误的位置查找.

Also make sure your code doesn't swallow exceptions. If you could have seen the error message in your code, you would have seen instantly that the code tried to look in the wrong place.

最后,这种方法很脆弱.如果路径更改,使用其他Web服务器以及许多其他情况,您的应用程序就会中断.

Lastly, this approach is brittle. Your app breaks if the path changes, when a different web server is used and for many other cases.

请考虑将webapp strsproperties 扩展为接受HTTP请求.这样,您可以将应用程序配置为通过URL连接到 strsproperties .这对任何Web服务器都适用,您甚至可以将属性放在其他主机上.

Consider extending the webapp strsproperties to accept HTTP requests. That way, you could configure your app to connect to strsproperties via an URL. This would work for any web server, you could even put the properties on a different host.

这篇关于如何使用Java从linux环境中获取tomcat中当前目录的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆