我已经查看了其他5个提出相同问题的问题,但它们都不起作用。我能够将资源文件夹部署到函数的可运行.jar所在的wwwroot。
/resources与.jar位于同一目录中。
我曾尝试将资源与jar路径捆绑在一起,并使用this.getClass().getResource('resources/file.txt')进行访问,但由于某些原因,它在云环境中不起作用,而且,我正在使用的File要求我使用原生Java。
作为参考,以下是部署Azure函数后结构的屏幕截图:

如上所述,该.jar文件需要访问/resources文件夹。
发布于 2021-06-21 23:08:47
我也遇到了同样的问题,为了让它在本地和azure上都能工作,我想出了这个解决方案:
String filename = "myfile.dat";
InputStream stream = null;
try {
stream = MyFunction.class.getClassLoader().getResourceAsStream(filename);
if (stream == null) {
ctx.getLogger().severe(format("file [%s] not found", certificateName));
ctx.getLogger().warning("assuming azure environment, using absolute path /home/site/wwwroot/" + filename);
stream = new FileInputStream("/home/site/wwwroot/" + filename);
}
//...rest of the code, catch clause...getClassLoader().getResourceAsStream流将正确读取您放置在src/main/resources中的文件,并且FileInputStream将查看部署中的绝对路径。
https://stackoverflow.com/questions/64864366
复制相似问题