Velocity引擎无法从远程共享文件夹加载模板。

3
我有以下代码
File temlateFile = new File( "D:/config/emails/MailBody.vm" );
    temlateFile.exists();
    VelocityEngine velocityEngine = new VelocityEngine();
    velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
    velocityEngine.setProperty("file.resource.loader.class", FileResourceLoader.class.getName());
    velocityEngine.setProperty("file.resource.loader.path", temlateFile.getParentFile().getAbsolutePath());
    velocityEngine.init();
    template = velocityEngine.getTemplate( temlateFile.getName() );

这个方法有效是因为它加载了本地文件系统中的文件。

当我把第一行更改为:

File temlateFile = new File( "//remote/config/emails/MailBody.vm" );

它不能工作。

org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'MailBody.vm'
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:474)
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:352)
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1533)
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1514)
    at org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:373)
    at com.actuate.iserver.mail.VelocityContent.<init>(VelocityContent.java:33)
    at com.actuate.iserver.mail.VolumeCreationMail.<init>(VolumeCreationMail.java:40)
    at com.actuate.iserver.mail.VolumeCreationMail.main(VolumeCreationMail.java:67)

在这两种情况下,temlateFile.exists() 总是返回 true。
有什么想法?
2个回答

2

找到了问题。那看起来像是一个速度(velocity)的 bug。

文件 temlateFile = new File( "//remote/config/emails/MailBody.vm" ); 能正常工作。

文件 temlateFile = new File( "\\remote\config\emails\MailBody.vm" ); 不能工作。


你实际上使用了 \\remote\config\emails\MailBody.vm 作为路径吗?这是无效的,因为 \ 是转义字符。或者你使用了 \\\\remote\\config\\emails\\MailBody.vm - Sergiu Dumitriu
@performanceuser 是对的,我的情况也是一样,谢谢。在探索中,我看到了URL \\aa\bb\cc\mail,但在代码中,我必须将URL更改为//aa/bb/cc/mail - vanduc1102

0
罪魁祸首是文件路径开头的两个反斜杠。它们没有被VelocityEngine正确处理。解决方法是将前导双反斜杠替换为双斜杠。
velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, temlateFile.getParent().replace("\\\\", "//"));

或者直接将它们全部替换掉

velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, temlateFile.getParent().replace("\\", "/"));

问题在1.7版本中仍然存在

new File( "//remote/config/emails/MailBody.vm" ); - 可能无法工作,因为file.getParent()可能会将路径转换为反斜杠。


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