如何从我的Java Web服务访问.properties文件

编程入门 行业动态 更新时间:2024-10-25 02:22:14
本文介绍了如何从我的Java Web服务访问.properties文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经使用tomcat成功部署了Java Web服务.该Web服务正在访问配置文件(.Properties),我已将config.properties文件放置在以下目录中

C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ProcurementWS1\WEB-INF\classes\MainPackagePr

ProcurmentWS1是WAR文件的名称

这是尝试从我的代码访问它的方式:

Properties properties = new Properties(); InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("procwsconfig.properties"); properties.load(is);

并且在加载时出现空指针异常

你能告诉我哪里做错了吗?

解决方案

将其放在src/config中直接放在源中的config包中,构建后将转到/WEB-INF/classes/config/config.properties

和this.getClass().getResourceAsStream("/config/config.properties");

然后我在具有方法的同一项目中创建了一个服务类.

public static InputStream getConfigAsInputStream(){ return Service.class.getResourceAsStream("/config/config.properties"); }

I have deployed my java web service successfully using tomcat. This web service is accessing a configuration file (.Properties) I have placed the config.properties files in the following directory

C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ProcurementWS1\WEB-INF\classes\MainPackagePr

ProcurmentWS1 is the name of the WAR file

this is how am trying to access it from my code:

Properties properties = new Properties(); InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("procwsconfig.properties"); properties.load(is);

And am getting a null pointer exception on load

can you tell me what am doing wrong?

解决方案

put it in src/config direct to config package in source , upon build it will go to /WEB-INF/classes/config/config.properties

and this.getClass().getResourceAsStream("/config/config.properties");

and then I created a Service Class in the same project which has a method.

public static InputStream getConfigAsInputStream(){ return Service.class.getResourceAsStream("/config/config.properties"); }

更多推荐

如何从我的Java Web服务访问.properties文件

本文发布于:2023-11-08 14:17:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569608.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   Java   Web   properties

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!