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

跟風遠走 提交于 2019-12-02 13:27:37

Let's assume you're using OpenNLP 1.5.3, then you should use another way of loading resource files, which uses no "hard" path references via URI conversion.

Given an environment in which within the directory WEB-INF another directory resources exists that contains your OpenNLP model files, you're code fragment should be written as follows:

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

See Javadoc for ServletContext#getResourceAsStream and this StackOverflow post.

Important Hint

Sadly, there are other issues with your code. The OpenNLP class POSModelLoader is only for internal use, see official Javadoc for POSModelLoader:

Loads a POS Tagger Model for the command line tools.

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

Therefore, loading a POSModel in a Web context should be done differently: Via one of the available constructors of that class. You can reformulate the above code fragment like so:

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

This way, you're compliant with the OpenNLP API and at the same time you do proper exception handling. Moreover, you can now use a Debugger in case something remains unclear with the resource path references of your model files.

Hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!