我正在尝试访问GeoLiteCity文件,它在我的本地计算机上工作,但无法获取云上的路径(Azure)。文件保存的层次结构如下:
/src/main/resources/database/GeoLiteCity.dat我的代码:
void getFile() {
if (this.file == null) {
try {
URL url = this.getClass().getClassLoader().getResource("database/GeoLiteCity.dat");
this.file = new File(url.toURI());
System.out.println(file.getPath().toString());
} catch (Exception ex) {
System.out.println(file.getPath().toString(),ex);
}
}
}发布于 2017-02-20 15:39:44
假设代码是运行在Tomcat上的Java的一部分。根据我的经验,如果您想通过this.getClass().getClassLoader().getResource("<the data file path relative to the root classes path>")方法获取资源,请确保webapps/<your-webapp-name>/WEB-INF/classes/database/路径下的数据文件GeoLiteCity.dat。
如果你的WebApp部署在Azure WebApp上,我想你可以尝试在Azure webapp上使用数据文件的绝对路径,比如D:/home/site/wwwroot/webapps/<your-webapp-name>/<the data file path>,并且只使用new File("<the absolute path of data file>")。
https://stackoverflow.com/questions/42273386
复制相似问题