如何在Camel流程中访问资源文件?

4

我正在开发在FUSE服务器上运行的骆驼应用程序,并尝试从项目中的资源文件夹中读取xsd文件,如下所示。但问题是我无法读取资源文件夹中文件的确切路径或文件内容。

enter image description here

我尝试在代码中读取 "Employee.xsd" 文件,但没有成功。
File file = new File(classLoader.getResource("Employee.xsd").getFile());
String fileContent=FileUtils.readFileToString(file);// Using Commons-IO

如果出现java.io.FileNotFoundException: File '\Employee.xsd' does not exist异常,有人能够解决这种问题吗?
2个回答

1

请将以下与编程相关的内容从英文翻译成中文。只返回翻译后的文本: - snerd

1

对于下一个开始使用camel并遇到此问题的人

如果您正在camel上下文中操作,似乎无法以通常的方式访问类路径资源(因此忽略Claus答案的第一部分)。相反,您需要像Claus建议的那样获取CamelContext并使用它。以下是在Processor的process方法中如何实现:

public void process(Exchange exchange) throws IOException {

    String getMockResponsePathString = exchange.getContext()
                                               .getClassResolver()
                                               .loadResourceAsURL({$MOCK_RESOURCE_NAME})  
                                               .getPath();

    Path mockResponsePath = Paths.get(mockResponsePathString);
    byte[] mockResponseBytes = Files.readAllBytes(mockResponsePath);
    String mockResponseString = new String(mockResponseBytes);

    exchange.getIn().setBody(mockResponseString);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接