java tomcat 图片路径,从Java访问Tomcat路径

I have a webapp running in my local Tomcat. The path is: tomcat/webapps/myproject.

I have some resources in this project all in a folder with this path: tomcat/webapps/myproject/resources

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

My Java code:

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

}

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.

解决方案

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

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

Edited

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.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值