gpt4 book ai didi

java - Java/Liferay 中的文件路径问题

转载 作者:行者123 更新时间:2023-11-30 07:57:16 24 4
gpt4 key购买 nike

我尝试了多种方法从资源文件夹加载属性文件。

每次,我都会遇到文件未找到的异常。我的代码如下:

 Properties prop = new Properties();
FileInputStream inputStream = new FileInputStream("/resource/excelfilepath.properties");
prop.load(inputStream);
String path = prop.getProperty("excelPath");
System.out.println("Excel File Path "+ path);

我的项目结构如下所示,

enter image description here

文件路径文字所需的结构是什么?

最佳答案

我不认为您真的想从网络资源中读取 ....properties 文件。这样,只要您不在 web.xml 中显式隐藏内容,所有访问您服务器的用户都可以看到该内容。

将其放入访问类旁边的类路径中更为常见。这样您就可以使用类加载器访问它,并且它对网络用户不再可见:

Properties prop = new Properties();
prop.load(CreateUser.class.getResourceAsStream("excelfilepath.properties"));

但是当您使用 Liferay 时,您也应该使用它的配置。只需将属性UserCreationPortlet.excelPath 添加到您的portal-ext.properties 并使用:

String path = PrefsPropsUtil.getString("UserCreationPortlet.excelPath", defaultPath);

关于java - Java/Liferay 中的文件路径问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32502219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com