Azure Functions Java 读取项目/资源中的文件?

huangapple go评论77阅读模式
英文:

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上都能正常工作。

英文:

How can I read a file inside a project ( resources folder ) when I host it as an azure function?

I tried with:

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;
}

However I get an exception:

[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.&lt;init&gt;(FileInputStream.java:157)
[09.08.2020 18:05:08]   at java.base/java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:112)

The filename or folder name is incorrect.

I testing it on the local machine. I would like to get it works on localhost and Azure.

答案1

得分: 1

以下是翻译好的代码部分:

public static String GetFileInStringFromResources(String pathToResources) throws IOException {

    // 这是在jar文件内的路径
    InputStream input = FilesHelper.class.getResourceAsStream("/resources/" + pathToResources);

    // 这是在IDE内部
    if (input == null) {
        input = FilesHelper.class.getClassLoader().getResourceAsStream(pathToResources);
    }

    // 在这里,你可以返回 (InputStream) input 或者作为字符串返回
    // return input;

    // 将InputStream转换为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");

}
英文:

Ok here is a working method:

public static String GetFileInStringFromResources(String pathToResources) throws IOException {

    // this is the path within the jar file
    InputStream input = FilesHelper.class.getResourceAsStream(&quot;/resources/&quot; + 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(&quot;UTF-8&quot;);

}

答案2

得分: 0

这是我在Groovy中使用的方法:

public String GetFileInStringFromResources(String pathToResource) throws IOException {
    // 这是jar文件内部的路径
    InputStream input = this.getClass().getResourceAsStream(pathToResource)

    // 这是从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

英文:

This is what worked for me in 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(&quot;UTF-8&quot;)
}

Note the resource needs to be saved in the official Maven resources directory.
https://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html

huangapple
  • 本文由 发表于 2020年8月10日 02:16:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63329781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定