Azure 函数 java 读取项目/资源中的文件?
Azure functions java read file inside a project / resources?
当我将项目(资源文件夹)托管为 azure 函数时,如何读取项目中的文件?
我试过:
public static File GetFileFromResources(String pathFromResources) {
File file;
ClassLoader classLoader = FilesHelper.class.getClassLoader();
URL url = classLoader.getResource(pathFromResources);
file = new File(url.getFile());
return file;
}
但是我得到一个例外:
[09.08.2020 18:05:08] Caused by: java.io.FileNotFoundException: C:\Users\Maciej-pc\Desktop\AzureFunctionsss\hl7edytorFunctions\target\azure-functions\hl7edytorFunctions-1596901248640\file:\C:\Users\Maciej-pc\Desktop\AzureFunctionsss\hl7edytorFunctions\target\azure-functions\hl7edytorFunctions-1596901248640\hl7edytorFunctions-1.0-SNAPSHOT.jar!\somethinng.xsl (Nazwa pliku, nazwa katalogu lub skladnia
etykiety woluminu jest niepoprawna)
[09.08.2020 18:05:08] at java.base/java.io.FileInputStream.open0(Native Method)
[09.08.2020 18:05:08] at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
[09.08.2020 18:05:08] at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
[09.08.2020 18:05:08] at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
文件名或文件夹名不正确。
我在本地机器上测试它。我想让它在本地主机和 Azure 上运行。
好的,这是一个工作方法:
public static String GetFileInStringFromResources(String pathToResources) throws IOException {
// this is the path within the jar file
InputStream input = FilesHelper.class.getResourceAsStream("/resources/" + pathToResources);
// here is inside IDE
if (input == null) {
input = FilesHelper.class.getClassLoader().getResourceAsStream(pathToResources);
}
//here you can return (InputStream) input or you can return as string
//return input;
// convert InputStream to String
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString("UTF-8");
}
这就是我在 Groovy 中工作的方法:
public String GetFileInStringFromResources(String pathToResource) throws IOException {
// this is the path within the jar file
InputStream input = this.getClass().getResourceAsStream(pathToResource)
// here is from inside IDE
if (input == null) {
input = this.getClass().getClassLoader().getResourceAsStream(pathToResource)
}
return input.getText("UTF-8")
}
注意资源需要保存在Maven官方资源目录下。
https://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html
当我将项目(资源文件夹)托管为 azure 函数时,如何读取项目中的文件?
我试过:
public static File GetFileFromResources(String pathFromResources) {
File file;
ClassLoader classLoader = FilesHelper.class.getClassLoader();
URL url = classLoader.getResource(pathFromResources);
file = new File(url.getFile());
return file;
}
但是我得到一个例外:
[09.08.2020 18:05:08] Caused by: java.io.FileNotFoundException: C:\Users\Maciej-pc\Desktop\AzureFunctionsss\hl7edytorFunctions\target\azure-functions\hl7edytorFunctions-1596901248640\file:\C:\Users\Maciej-pc\Desktop\AzureFunctionsss\hl7edytorFunctions\target\azure-functions\hl7edytorFunctions-1596901248640\hl7edytorFunctions-1.0-SNAPSHOT.jar!\somethinng.xsl (Nazwa pliku, nazwa katalogu lub skladnia
etykiety woluminu jest niepoprawna)
[09.08.2020 18:05:08] at java.base/java.io.FileInputStream.open0(Native Method)
[09.08.2020 18:05:08] at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
[09.08.2020 18:05:08] at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
[09.08.2020 18:05:08] at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
文件名或文件夹名不正确。
我在本地机器上测试它。我想让它在本地主机和 Azure 上运行。
好的,这是一个工作方法:
public static String GetFileInStringFromResources(String pathToResources) throws IOException {
// this is the path within the jar file
InputStream input = FilesHelper.class.getResourceAsStream("/resources/" + pathToResources);
// here is inside IDE
if (input == null) {
input = FilesHelper.class.getClassLoader().getResourceAsStream(pathToResources);
}
//here you can return (InputStream) input or you can return as string
//return input;
// convert InputStream to String
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString("UTF-8");
}
这就是我在 Groovy 中工作的方法:
public String GetFileInStringFromResources(String pathToResource) throws IOException {
// this is the path within the jar file
InputStream input = this.getClass().getResourceAsStream(pathToResource)
// here is from inside IDE
if (input == null) {
input = this.getClass().getClassLoader().getResourceAsStream(pathToResource)
}
return input.getText("UTF-8")
}
注意资源需要保存在Maven官方资源目录下。 https://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html