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
请,如何修复错误然后下载文件。
答案 0 :(得分:0)
在Jetty能够完成将其响应数据写回客户端之前,您使用连接到Jetty的任何Web客户端都关闭了与Jetty的连接。这是你看到的例外细节。
您声明此客户端位于Android设备上。
请注意: