为什么无法从 WEB-INF 文件夹中加载 POSModel 文件?

Why does the loading of a POSModel file not work from inside the WEB-INF folder?

我正在为我的 Web 项目使用 Spring MVC。我将模型文件放在 WEB-INF 目录中

String taggerModelPath = "/WEB-INF/lib/en-pos-maxent.bin";
String chunkerModelPath = "/WEB-INF/lib/en-chunker.bin";

POSModel model = new POSModelLoader()
.load(new File(servletContext.getResource(taggerModelPath).toURI().getPath()));

这个工作 Windows 环境。但是,当我将它部署到我的远程 Linux 服务器上时,出现错误

HTTP 状态 500 - 请求处理失败;嵌套异常是 opennlp.tools.cmdline.TerminateToolException:POS Tagger 模型文件不存在!路径:/localhost/nlp/WEB-INF/lib/en-pos-maxent.bin

访问文件资源的最佳方式是什么?谢谢

假设您使用的是 OpenNLP 1.5.3,那么您应该使用另一种加载资源文件的方法,该方法不使用通过 URI 转换的“硬”路径引用。

给定一个环境,其中目录 WEB-INF 中存在另一个目录 resources,其中包含您的 OpenNLP 模型文件,您的代码片段应编写如下:

String taggerModelPath = "/WEB-INF/resources/en-pos-maxent.bin";
String chunkerModelPath= "/WEB-INF/resources/en-chunker.bin";

POSModel model = new POSModelLoader().load(servletContext.getResourceAsStream(taggerModelPath));

请参阅 ServletContext#getResourceAsStream and this Whosebug post 的 Javadoc。

重要提示

遗憾的是,您的代码还有其他问题。 OpenNLP class POSModelLoader 仅供 内部 使用,参见官方 Javadoc POSModelLoader:

Loads a POS Tagger Model for the command line tools.

Note: Do not use this class, internal use only!

因此,在 Web 上下文中加载 POSModel 应该以不同的方式完成:通过可用的 constructors of that class 之一。您可以像这样重新表述上面的代码片段:

try {
    InputStream in = servletContext.getResourceAsStream(taggerModelPath);
    POSModel posModel;
    if(in != null) {
        posModel = new POSModel(in);
        
        // from here, <posModel> is initialized and you can start playing with it...
        // ...
    }
    else {
        // resource file not found - whatever you want to do in this case
    }
}
catch (IOException | InvalidFormatException ex) {
    // proper exception handling here... cause: getResourcesAsStream or OpenNLP...
} 

这样,您就符合 OpenNLP API 并且同时进行了适当的异常处理。此外,您现在可以使用调试器,以防您的模型文件的资源路径引用仍然不清楚。

希望对您有所帮助。