15

While executing this code,

ServletOutputStream out = response.getOutputStream();
out.write(bytes, 0, bytes.length);
out.flush();  
out.close();  

I'm getting this exception frequently and I can't figure out why it's happening.

Caused by: ClientAbortException:  java.net.SocketException: Broken pipe
    at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:358)
    at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:354)
    at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:381)
    at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:370)
    at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:89)

I have a web app running in a linux server, under JBoss 4.2.3 and MySQL.

How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
gdfbarbosa
  • 825
  • 3
  • 10
  • 21
  • That means the client of your servlet hung up the connection for some reason. – Keith Randall Aug 30 '12 at 22:56
  • What kind of reason is that? This is an action for a file download, for example. – gdfbarbosa Aug 30 '12 at 23:41
  • 1
    Right, maybe the browser that was doing the file download crashed halfway through. Or their internet connection went down. Or whatever, the receiver can't accept the bytes you're writing to it. – Keith Randall Aug 30 '12 at 23:46
  • possible duplicate of [ClientAbortException at application deployed at jboss with IE8 browser](http://stackoverflow.com/questions/9788891/clientabortexception-at-application-deployed-at-jboss-with-ie8-browser), [JSF1064: Unable to find or serve resource, jquery.js Tomcat 7 + RichFaces 4](http://stackoverflow.com/questions/10490028/jsf1064-unable-to-find-or-serve-resource-jquery-js-tomcat-7-richfaces-4) and more. – BalusC Aug 31 '12 at 00:48
  • BalusC, thanks for the reply. I`ll try ignoring the exception since I can`t control it. – gdfbarbosa Aug 31 '12 at 10:03

2 Answers2

11

Typically this is due to the client disconnecting. There are several reasons the client may have disconnected (timed out, browser was closed, etc). Also, as general practice, if you didn't open the stream then don't close. More on closing the stream here Should I close the servlet outputstream?.

Community
  • 1
  • 1
km1
  • 2,383
  • 1
  • 22
  • 27
3

This can be improved by adding proper response.setBufferSize(bufferSizeneeded+200); The default buffer size is 8124 , Which may not be applicable for all the users ! and response.setHeader("Content-Length", String.valueOf(exportData.length));