Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么从WEB-INF文件夹中加载POSModel文件不起作用?_Java_Tomcat_Servlets_Opennlp_Resource Loading - Fatal编程技术网

Java 为什么从WEB-INF文件夹中加载POSModel文件不起作用?

Java 为什么从WEB-INF文件夹中加载POSModel文件不起作用?,java,tomcat,servlets,opennlp,resource-loading,Java,Tomcat,Servlets,Opennlp,Resource Loading,我正在使用SpringMVC进行我的web项目。我将模型文件放在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(taggerMode

我正在使用SpringMVC进行我的web项目。我将模型文件放在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.TerminateToOleException:POS标记器模型文件不存在!路径:/localhost/nlp/WEB-INF/lib/en-pos-maxent.bin


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

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

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

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));
请参阅Javadoc以了解更多信息

重要提示

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

为命令行工具加载POS标记器模型

注意:请勿使用此类,仅限内部使用

因此,在Web上下文中加载
POSModel
的方式应该有所不同:通过一个可用的。您可以将上述代码片段重新格式化如下:

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...
} 
试试看{
InputStream in=servletContext.getResourceAsStream(taggerModelPath);
POSModel-POSModel;
if(in!=null){
posModel=新的posModel(in);
//从这里,是初始化,你可以开始玩它。。。
// ...
}
否则{
//找不到资源文件-无论您在这种情况下想做什么
}
}
捕获(IOException | InvalidFormatException ex){
//这里正确的异常处理…原因:getResourcesAsStream或OpenNLP。。。
} 
这样,您就符合OpenNLP API,同时进行适当的异常处理。此外,您现在可以使用调试器,以防模型文件的资源路径引用仍然不清楚


希望有帮助。

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

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

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));
请参阅Javadoc以了解更多信息

重要提示

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

为命令行工具加载POS标记器模型

注意:请勿使用此类,仅限内部使用

因此,在Web上下文中加载
POSModel
的方式应该有所不同:通过一个可用的。您可以将上述代码片段重新格式化如下:

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...
} 
试试看{
InputStream in=servletContext.getResourceAsStream(taggerModelPath);
POSModel-POSModel;
if(in!=null){
posModel=新的posModel(in);
//从这里,是初始化,你可以开始玩它。。。
// ...
}
否则{
//找不到资源文件-无论您在这种情况下想做什么
}
}
捕获(IOException | InvalidFormatException ex){
//这里正确的异常处理…原因:getResourcesAsStream或OpenNLP。。。
} 
这样,您就符合OpenNLP API,同时进行适当的异常处理。此外,您现在可以使用调试器,以防模型文件的资源路径引用仍然不清楚


希望有帮助。

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

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

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));
请参阅Javadoc以了解更多信息

重要提示

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

为命令行工具加载POS标记器模型

注意:请勿使用此类,仅限内部使用

因此,在Web上下文中加载
POSModel
的方式应该有所不同:通过一个可用的。您可以将上述代码片段重新格式化如下:

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...
} 
试试看{
InputStream in=servletContext.getResourceAsStream(taggerModelPath);
POSModel-POSModel;
if(in!=null){
posModel=新的posModel(in);
//从这里,是初始化,你可以开始玩它。。。
// ...
}
否则{
//找不到资源文件-无论您在这种情况下想做什么
}
}
捕获(IOException | InvalidFormatException ex){
//这里正确的异常处理…原因:getResourcesAsStream或OpenNLP。。。
} 
这样,您就符合OpenNLP API,同时进行适当的异常处理。此外,您现在可以使用调试器,以防模型文件的资源路径引用仍然不清楚


希望有帮助。

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

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

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