设置:我已经设置了嵌入式码头(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();
这段代码最初是从这里开始的。
当我导航到地址http:// localhost:9090 /时,我看到目录中列出的文件,并且能够单击并打开单个文本文件。
问题:出于某种无法解释的原因,只有当我单击一个0字节的文件(也就是一个空文件,但仍在浏览器中显示)时,该连接才会尝试加载,但最终超时(30秒),并且我得到了响应Safari中说“服务器意外断开了连接”。另外,当我HttpURLConnection
对0字节文件进行处理时,返回的内容长度为-1;当返回0时,返回的内容长度为0。当然,这仅适用于空文件。
在独立Jetty中看到的预期行为:当我使用独立Jetty并提供相同的文件时,我能够“打开”空文件,该文件仅在Web浏览器中返回空白页。使用时HttpURLConnection
,我得到的内容长度为0。
尽管这似乎是一个“毫无意义”的任务,但是一台服务器正在以编程方式与嵌入式码头服务器进行同步(因此,我希望这些空文件进行同步)。我想这与资源处理程序看到0个字节有关,因为它提供了静态内容,但是我不太确定如何像现在一样获得独立码头服务器的行为,在尝试拉动静态服务器时会出错。空文件。
谢谢!
您的代码至少可以在Jetty 9.2.7.v20140116上正常运行
我使用的完整示例:
package jetty;
import java.io.File;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
public class SimpleResources
{
public static void main(String[] args)
{
Server server = new Server(9090);
String resourceBase = System.getProperty("resourceBase", ".");
System.err.printf("Resource Base is: %s%n", new File(resourceBase).getAbsolutePath());
ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setDirectoriesListed(true);
resource_handler.setWelcomeFiles(new String[] { "index.html" });
resource_handler.setResourceBase(resourceBase);
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
server.setHandler(handlers);
try
{
server.start();
server.join();
}
catch (Throwable t)
{
t.printStackTrace(System.err);
}
}
}
我运行它,将-DresourceBase
系统属性指向具有以下内容的目录...
$ ls -la
total 8
drwxrwxr-x. 2 joakim joakim 4096 Jan 20 11:53 .
drwxrwxr-x. 3 joakim joakim 4096 Jan 20 11:53 ..
-rw-rw-r--. 1 joakim joakim 0 Jan 20 11:53 foo.txt
而一旦运行控制台显示...
2015-01-20 11:55:07.788:INFO::main: Logging initialized @68ms
Resource Base is: /home/joakim/code/Jetty/empties
2015-01-20 11:55:07.837:INFO:oejs.Server:main: jetty-9.2.7.v20150116
2015-01-20 11:55:07.860:INFO:oejs.ServerConnector:main: Started ServerConnector@5461eda{HTTP/1.1}{0.0.0.0:9090}
2015-01-20 11:55:07.861:INFO:oejs.Server:main: Started @144ms
像这样的测试要求...
$ curl --dump-header - http://localhost:9090/foo.txt
HTTP/1.1 200 OK
Date: Tue, 20 Jan 2015 18:55:39 GMT
Content-Type: text/plain
Content-Length: 0
Server: Jetty(9.2.7.v20150116)
更新:
按原样工作,并且不对以下版本的码头进行任何修改(未对版本进行详尽的测试,也仅对一些较旧的版本进行了详尽的测试)
9.2.6.v20141205
-相同的结果9.2.4.v20141103
-相同的结果9.2.1.v20140609
-相同的结果9.1.5.v20140505
-Date
响应标头中没有,其余部分相同(是的,它也发送Content-Length: 0
)本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句