【问题标题】:Access from Java to Tomcat paths从 Java 到 Tomcat 路径的访问
【发布时间】:2010-10-07 08:15:33
【问题描述】:


我在本地 Tomcat 中运行了一个 webapp。路径为:tomcat/webapps/myproject.
我在这个项目中有一些资源都在这个路径的文件夹中:tomcat/webapps/myproject/resources

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

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

我还尝试了 /\ 的不同组合,但在运行我的项目时出现此错误:

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");                  
} 

我不知道出了什么问题,在我之前做的项目中,与此类似,我有类似的东西,它找到了一切。
有任何想法吗??提前致谢。

【问题讨论】:

  • 好的,我重新提出我的问题。你在哪里保存你的 Java 代码?基本上你的 webapp 的文件夹结构是什么。
  • 我想做的项目,只是为我的webapp项目提供服务。但是真的很没意思吗??我的意思是,我在 config.properties 中连接到我的本地主机,对吗?感谢您的评论和回答

标签: java tomcat configuration path


【解决方案1】:

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

我不知道您是如何访问该文件的,但以防万一我建议您使用 PropertiesConfiguration

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

已编辑

问题是当您的文件位于(可能)/var/lib/tomcat6/webapps/myproject/resources 时,您正试图打开位于/myproject/resources 的文件(请记住“/”是您的根文件系统)。

您可以使用ServletContext#getRealPath获取文件的真实路径

【讨论】:

  • 是的,我正在这样做。但是在 tomcat/webapps/myproject 中没有 src,只有文件夹作为页面,样式...
  • 但这正是我想要避免的,绝对路径为 /var/lib/tomcat6/webapps/myproject/resources。我想通过 localhost 或其他主机获取这些资源,而不依赖于机器。谢谢
  • 这就是为什么你应该使用ServletContext#getRealPath 这样你就不用依赖机器了!
猜你喜欢
  • 2010-09-08
  • 1970-01-01
  • 1970-01-01
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-24
  • 1970-01-01
相关资源
最近更新 更多