Solved

JSP response while setting error

Posted on 2013-05-30
2
659 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

630 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