Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JSP response while setting error

Posted on 2013-05-30
2
Medium Priority
?
662 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 2000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

688 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