Solved

JSP response while setting error

Posted on 2013-05-30
2
646 Views
Last Modified: 2013-05-31
I'm using the below JSP code to write a file back to the end user.

<html>
<head>
<title>Goto</title>
</head>
<body>
<% 
 ....	   
 IDfSysObject sysObj = (IDfSysObject) session1.getObject(new DfId("09001eab80002993"));
 String strFileName = sysObj.getObjectName();
 String strFileExtension = sysObj.getFormat().getDOSExtension();

 if(!strFileName.endsWith("." + strFileExtension))
 {
 strFileName = strFileName + "." + strFileExtension;
 }

 ByteArrayInputStream bais = sysObj.getContent();
 response.setContentType("application/x-download");
 response.setHeader("Content-Disposition", "attachment; filename=" + strFileName);


 int flength = bais.available();
 ByteArrayOutputStream ostream = new ByteArrayOutputStream();
 for(int i=0;i<flength;i++)
 {
 ostream.write(bais.read());
 }

 ostream.writeTo(response.getOutputStream());
 response.getOutputStream().flush();
 response.getOutputStream().close();
%>
</body>
</html>

Open in new window


This works fine but gives the below error in the log file -

31-May-2013 05:48:23.734 SEVERE [tomcat-http--46] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [jsp] in context with path [/MAN_DVR] threw exception [An exception occurred processing JSP page /downloadContent.jsp at line 56

53:  ostream.write(bais.read());
54:  }
55: 
56:  ostream.writeTo(response.getOutputStream());
57:  response.getOutputStream().flush();
58:  response.getOutputStream().close();
59: 


Stacktrace:] with root cause
 java.net.SocketException: Software caused connection abort: socket write error
	at java.net.SocketOutputStream.socketWrite0(Native Method)
	at java.net.SocketOutputStream.socketWrite(Unknown Source)
	at java.net.SocketOutputStream.write(Unknown Source)
	at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:215)
	at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:462)
	at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:366)
	at org.apache.coyote.http11.InternalOutputBuffer$OutputStreamOutputBuffer.doWrite(InternalOutputBuffer.java:240)

Open in new window


Are there some corrections needed in the code?

Thank you
0
Comment
Question by:ank5
  • 2
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39210010
int flength = bais.available();

Open in new window


You're lucky you have an exception, as that code is wrong. You need to read (carefully) the api docs for InputStream.available. You can't use it to get the content length. You need to read the stream until EOF. See http://technojeeves.com/joomla/index.php/free/51-copying-streams and copyStream. The ByteArrayOutputStream is redundant - just copy directly to the response's output stream.

IDfSysObject should probably have a method to get the size on the file system, i'd guess, in which case, you could set the Content-Length header.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39210381
:)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strCount chalenge 3 50
strDist challenge 35 84
Eclipse Neon and jdk 1.8.0 11 69
groovy example issue 10 67
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now