Jetty 9服务器连接被远程主机强行关闭

时间:2015-01-09 12:09:01

标签: android spring io jetty

Spring3.2.9.RELEASE,Jetty9.2.4或Jetty8.1.16

下载文件代码

@RequestMapping(value = "/file", method = RequestMethod.GET)
public void file(@RequestParam("id") String path, HttpServletResponse response) throws IOException {

FileEntity file = attachService.getAttachFileByPath(path);
OutputStream out = new BufferedOutputStream(response.getOutputStream());
try {
response.reset();
response.setContentType("application/octet-stream; charset=utf-8");
response.setContentLength(file.getFileBytes().length);
response.addHeader("Content-disposition", "attachment; filename=" + file.getFileName());
response.addHeader("Cache-Control", "no-cache");
out.write(file.getFileBytes());
out.flush();
out.close();
} finally {
if (out != null) {
out.close();
}
}
}

然后我在下面的代码中使用相同的错误。

HttpHeaders headers = new HttpHeaders();  
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);  
headers.setContentDispositionFormData("attachment", file.getFileName());  
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file.getFileBytes()), headers, HttpStatus.CREATED);

代码使用Android浏览器下​​载文件可以很好地完成,但后来我在日志文件中发现了错误。

org.eclipse.jetty.io.EofException: null
    at org.eclipse.jetty.io.ChannelEndPoint.flush(ChannelEndPoint.java:192) ~[na:na]
    at org.eclipse.jetty.io.WriteFlusher.flush(WriteFlusher.java:441) ~[na:na]
    at org.eclipse.jetty.io.WriteFlusher.completeWrite(WriteFlusher.java:399) ~[na:na]
    at org.eclipse.jetty.io.SelectChannelEndPoint.onSelected(SelectChannelEndPoint.java:111) ~[na:na]
    at org.eclipse.jetty.io.SelectorManager$ManagedSelector.processKey(SelectorManager.java:636) ~[na:na]
    at org.eclipse.jetty.io.SelectorManager$ManagedSelector.select(SelectorManager.java:607) ~[na:na]
    at org.eclipse.jetty.io.SelectorManager$ManagedSelector.run(SelectorManager.java:545) ~[na:na]
    at org.eclipse.jetty.util.thread.NonBlockingThread.run(NonBlockingThread.java:52) ~[na:na]
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:620) ~[na:na]
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:540) ~[na:na]
    at java.lang.Thread.run(Thread.java:745) ~[na:1.7.0_71]
Caused by: java.io.IOException: An existing connection was forcibly closed by the remote host
    at sun.nio.ch.SocketDispatcher.write0(Native Method) ~[na:1.7.0_71]
    at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51) ~[na:1.7.0_71]
    at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93) ~[na:1.7.0_71]
    at sun.nio.ch.IOUtil.write(IOUtil.java:65) ~[na:1.7.0_71]
    at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:487) ~[na:1.7.0_71]
    at org.eclipse.jetty.io.ChannelEndPoint.flush(ChannelEndPoint.java:170) ~[na:na]
    ... 10 common frames omitted

请,如何修复错误然后下载文件。

1 个答案:

答案 0 :(得分:0)

在Jetty能够完成将其响应数据写回客户端之前,您使用连接到Jetty的任何Web客户端都关闭了与Jetty的连接。这是你看到的例外细节。

您声明此客户端位于Android设备上。

请注意:

  • 移动网络不被视为可靠。
  • 切换网络的设备(如从移动设备到wifi)将切断其活动连接。
  • 如果您在移动网络上从一个塔移动到另一个塔,也可以切断活动连接。
  • 移动网络提供商拥有积极的透明代理,往往会加剧这些问题。