无法使用Jetty从Windows文件夹提供文件

时间:2017-01-02 14:51:16

标签: java servlets io cross-platform

我修改了the embedded-jetty project来创建a stand-alone jsp-viewer,它应该能够提供任何包含jsps的目录,而不仅仅是嵌入在jar文件中的目录。 生成的jar在macOS和Linux上运行良好。

我发现它在Windows上不起作用 - 由于某种原因让我感到厌烦,尽管日志表明它解决了目录就好了。我已启用"dirAllowed"属性列出目录内容,但无论目录或文件如何,我都会一直收到此错误:

HTTP ERROR 404

Problem accessing /. Reason:

    Not Found

我删除了许多不需要的代码,但我相信我在Jetty演示项目中的项目之间的主要区别在于这个差异显示了我的项目如何引用webroot属性作为要服务的目录,而不是原始

中jar文件中的硬编码路径
>         String userDir = System.getProperty("webroot");
>         String webroot = (userDir != null && userDir.length() > 0)? userDir : ".";
>         webroot = Paths.get(webroot).toAbsolutePath().normalize().toString();

和这个

<         URL indexUri = this.getClass().getResource(WEBROOT_INDEX);
>         URL indexUri = new URL("file://" + webroot);

是否有关于file://语法的内容,我应该在Windows上以不同的方式处理它?<​​/ p>

在macOS上运行时从控制台输出

./jsp-viewer/view-jsp /tmp
2017-01-02 15:53:50.456:INFO::main: Logging initialized @124ms to org.eclipse.jetty.util.log.StdErrLog
jan 02, 2017 3:53:50 PM com.github.fatso83.jspviewer.Main defaultServletHolder
INFO: Base URI: file:/tmp
2017-01-02 15:53:50.574:INFO:oejs.Server:main: jetty-9.4.z-SNAPSHOT
2017-01-02 15:53:50.737:INFO:oejs.session:main: DefaultSessionIdManager workerName=node0
2017-01-02 15:53:50.737:INFO:oejs.session:main: No SessionScavenger set, using defaults
2017-01-02 15:53:50.740:INFO:oejs.session:main: Scavenging every 660000ms
2017-01-02 15:53:50.756:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@6d8a00e3{/,file:///tmp/,AVAILABLE}
2017-01-02 15:53:50.779:INFO:oejs.AbstractConnector:main: Started ServerConnector@457e2f02{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2017-01-02 15:53:50.779:INFO:oejs.Server:main: Started @451ms
jan 02, 2017 3:53:50 PM com.github.fatso83.jspviewer.Main getServerUri
INFO: Server URI: http://localhost:8080/

Windows上的输出(Mingw)

$ ./jsp-viewer/view-jsp /tmp/
2017-01-02 16:12:15.541:INFO::main: Logging initialized @266ms to org.eclipse.jetty.util.log.StdErrLog
jan 02, 2017 4:12:18 PM com.github.fatso83.jspviewer.Main defaultServletHolder
INFO: Base URI: file://C:/Users/SA_CAR~1.KOP/AppData/Local/Temp/2
2017-01-02 16:12:18.106:INFO:oejs.Server:main: jetty-9.4.z-SNAPSHOT
2017-01-02 16:12:18.449:INFO:oejs.session:main: DefaultSessionIdManager workerName=node0
2017-01-02 16:12:18.450:INFO:oejs.session:main: No SessionScavenger set, using defaults
2017-01-02 16:12:18.454:INFO:oejs.session:main: Scavenging every 600000ms
2017-01-02 16:12:18.475:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@24a67{/,file://C/Users/SA_CAR~1.KOP/AppData/Local/Temp/2,AVAILABLE}
2017-01-02 16:12:18.515:INFO:oejs.AbstractConnector:main: Started ServerConnector@feb3fa{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2017-01-02 16:12:18.530:INFO:oejs.Server:main: Started @3261ms
jan 02, 2017 4:12:18 PM com.github.fatso83.jspviewer.Main getServerUri
INFO: Server URI: http://localhost:8080/

1 个答案:

答案 0 :(得分:0)

错误在于手动创建Uris和Urls。通过将这项工作委托给Java,我获得了适用于所有操作系统的URL。如所怀疑的那样,错误出现在上述一行中:

我交换了

URL indexUri = new URL("file://" + webroot);

代表

Paths.get(webroot).toAbsolutePath().toUri().toURL()

一切都开始在Windows上运行了。 Jan Bartel在[jetty-users] mailing list提出了答案。