java - 在 netbeans + glassfish 项目中找不到文件

标签 java rest netbeans glassfish server

这是我的文件夹项目
enter image description here

我想读取我项目的 web 目录中的文件 book-form.html 并将其放入字符串中。

这就是我调用函数“getFileContent”的方式:

String content = getFileContent("web/book-form.html");

这是函数:

public String getFileContent(String filePath){
        String line, content = new String();
        try {
                File file = new File(filePath);
                FileReader fr = new FileReader(file);
                BufferedReader br = new BufferedReader(fr);
                while((line = br.readLine()) != null){
                        content += line;
                }
                br.close();
            fr.close();
        } catch(IOException e){
                System.out.println(e.getMessage());
        }

        return content;
    }

我的问题是 netbeans 告诉我它找不到我的文件 book-form.html

有什么想法吗?

最佳答案

File path to resource in our war/WEB-INF folder?

此外,您应该在最后一个 block 中关闭流或使用 try-with-resource如果你使用jdk 7+

关于java - 在 netbeans + glassfish 项目中找不到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29497059/

相关文章:

python - 使用 FLASK 将 REST API 与 Web 界面结合的最佳实践

java - Netbeans GUI 生成器 : how to edit generated code

java - 如何包含jpcap库?

java - 为什么 setVisible() 会删除我的组件?

java - 实现 SparseMatrix 的有效方法

c# - 使用 native C# API 管理我的 Azure 云服务?

java - Netbeans 生成的代码

java - 使用变量作为 x、y 参数在面板中以圆为中心

java - 判断string是否可以转为int数组

http - RESTful登录失败 : Return 401 or Custom Response