Solved

JSP response while setting error

Posted on 2013-05-30
2
656 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 36
How to get all the API from website? 11 123
Glassfish admin console not working 1 55
DTD and JAVA versions 1 27
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

685 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