【发布时间】:2015-03-18 05:09:07
【问题描述】:
设置:我已经设置了嵌入式码头(v9.1)以使用setDirectoriesListed(true) 提供静态文件,我使用的代码如下:
// Create a basic Jetty server object that will listen on port 8080. Note that if you set this to port 0
// then a randomly available port will be assigned that you can either look in the logs for the port,
// or programmatically obtain it for use in test cases.
Server server = new Server(9090);
// Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
// a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
ResourceHandler resource_handler = new ResourceHandler();
// Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
// In this example it is the current directory but it can be configured to anything that the jvm has access to.
resource_handler.setDirectoriesListed(true);
resource_handler.setWelcomeFiles(new String[]{ "index.html" });
resource_handler.setResourceBase(".");
// Add the ResourceHandler to the server.
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
server.setHandler(handlers);
// Start things up! By using the server.join() the server thread will join with the current thread.
// See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
server.start();
server.join();
此代码最初来自here。
当我导航到地址 http://localhost:9090/ 时,我会看到目录中列出的文件,并且可以单击并打开单个文本文件。
问题: 出于某种莫名其妙的原因,只有当我点击一个 0 字节的文件(又名它是一个空文件,但仍显示在浏览器中)时,连接才会尝试加载,但最终会多次出(30 秒),我在 safari 中收到回复说“服务器意外断开连接”。另外,当我对 0 字节文件创建 HttpURLConnection 时,我得到返回的内容长度为 -1;这当然只适用于空文件。
在独立 Jetty 中看到的预期行为:当我使用独立 Jetty 并提供相同的文件时,我能够“打开”在网络浏览器中仅返回空白页面的空文件。使用HttpURLConnection 时,我得到的内容长度为 0。
虽然这似乎是一项“毫无意义”的任务,但一台服务器正在以编程方式与嵌入式码头服务器同步(所以我希望这些空文件同步)。我想这与资源处理程序在提供静态内容时看到 0 字节有关,但我不太确定如何获得与现在独立码头服务器相同的行为,尝试拉动时出错空文件。
谢谢!
【问题讨论】:
标签: jetty embedded-jetty