Solved

JSP response while setting error

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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java 8 to Java 6 8 44
import as existing maven project 3 78
junit example issue 2 59
Github api to fetch the number of collaborators in a git repo 1 70
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

752 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