简体   繁体   English

Java:在HttpServlet中写入时发生IOException

[英]Java: IOException on write in HttpServlet

I've written a proxy of sorts in Java (and Jetty). 我已经用Java(和Jetty)编写了各种代理。 Anyway, it works great, but sometimes 无论如何,它都很棒,但有时

...
final OutputStream realOs = res.getOutputStream();
...
InputStream is = url.openStream();
int i;
while ((i = is.read(buffer)) != -1) {
  realOs.write(buffer, 0, i);
}

fails with IOException. 失败,出现IOException。 I've noticed that it mostly happens with large binary files, ie flash and Safari browser... 我注意到它主要发生在大型二进制文件中,例如Flash和Safari浏览器...

I'm puzzled... 我很困惑

This can happen if the browser is closed (or the user cancels the download) while you're still writing to the socket. 如果在您仍在向套接字写入数据时关闭了浏览器(或用户取消了下载),则会发生这种情况。 The browser closes the socket, so your OutputStream no longer has anything to write to. 浏览器将关闭套接字,因此您的OutputStream不再有任何写入内容。

Unfortunately it's hard to tell for sure whether this is really the case - in which case it's not an issue - or whether there's something more insidious going on. 不幸的是,很难确定是否确实如此-在这种情况下这不是问题-还是有更多阴险的事情正在发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM