从Java访问Tomcat路径 [英] Access from Java to Tomcat paths

查看:460
本文介绍了从Java访问Tomcat路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在我的本地Tomcat中运行了一个webapp。路径是: tomcat / webapps / myproject

我在这个项目中有一些资源全部在一个文件夹中,路径如下: tomcat / webapps / myproject / resources

所以,我试图从一个Java项目访问这个资源,使用一个文件 config.properties 。在这个文件中我有这样的:

So, I am trying to acces to this resources from a Java project, using a file config.properties. In this file I have something like this:

tomcat.url= http://localhost
tomcat.port=8080
tomcat.resources=/myproject/resources

我尝试了不同的组合 / \ ,但运行我的项目时遇到此错误:

I tryed also different combinations of / or \ but I get this error running my project:

Trying to acces to a directory that does not exist

我的Java代码:

Configuration config = new PropertiesConfiguration("config.properties");    
String sourcePath = config.getString("tomcat.resources");
//And I try to list this folder  
File dir = new File(sourcePath);
String[] children = dir.list();         
if (children == null) {          
    // Either dir does not exist or is not a directory
    throw new ServiceExecutionException("Trying to generate Metadata in a directory that does not exist");                  
} 



我不知道什么是错误的,在我之前做的项目,simliar对此,我有类似的东西,它找到了一切。

任何想法?提前感谢。

I don't know what is wrong, in projects I made before, simliar to this, I had something similar and it found everything.
Any ideas?? Thanks in advance.

推荐答案

文件必须位于 tomcat / webapps / myproject / src / main / resources 检查更新。

the file must be located at tomcat/webapps/myproject/src/main/resources Check update.

我不知道你是如何访问文件的,我建议您使用 PropertiesConfiguration

I don't know how you are accessing the file, but just in case I'd recommend you to use PropertiesConfiguration

 private static final String CONFIGURATION_PATH = "config.properties";
 PropertiesConfiguration configuration = new PropertiesConfiguration(CONFIGURATION_PATH);

已编辑

问题是,你试图打开一个文件位于(请记住,/是你的根文件系统)在 / myproject / resources 位于(可能) / var / lib / tomcat6 / webapps / myproject / resources

The problem is that you are trying to open a file located (keeping in mind that "/" is your root file system) at /myproject/resources when your file is located at (probably) /var/lib/tomcat6/webapps/myproject/resources.

您的文件的真实路径使用 ServletContext#getRealPath

You can get the real path of your file using ServletContext#getRealPath

这篇关于从Java访问Tomcat路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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