aspose file tools*
The moose likes I/O and Streams and the fly likes getOutputStream() has already been called for this response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "getOutputStream() has already been called for this response" Watch "getOutputStream() has already been called for this response" New topic
Author

getOutputStream() has already been called for this response

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
The following code is used to download file types....my file is being downloaded but at server side i am getting error like

[ERROR] [default] - Servlet.service() for servlet default threw exception <java.lang.IllegalStateException: getOutputStream() has already been called for this response>java.lang.IllegalStateException: getOutputStream() has already been called for this response


<%@page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page pageEncoding="UTF-8" %>
<%@ taglib uri="/struts-tags" prefix="struts"%>
<%@ page import="java.io.*"%>
<%
String sFile = (String) request.getAttribute("sFilePath");
System.out.println("sfilepath in jsp file is........."+sFile);
String mimeType = getMimeType(sFile);
response.setContentType(mimeType);
response.setHeader("Content-Disposition", "inline;filename=\"" + sFile + "\"");
FileInputStream in = null;
OutputStream output=null;
try {
output = response.getOutputStream();
in = new FileInputStream(sFile);
byte[] buffer = new byte[5 * 1024];
int size = 0;
while ((size = in.read(buffer, 0, buffer.length)) > 0) {
output.write(buffer, 0, size);
}


} catch (Exception e) {



}


finally {
in.close();
output.flush();
output.close();
}
if (true)
return;


%>
<%!
static String getMimeType (String fName) {
fName = fName.toLowerCase();
if (fName.endsWith(".jpg") || fName.endsWith(".jpeg") || fName.endsWith(".jpe")) return "image/jpeg";
else if (fName.endsWith(".gif")) return "image/gif";
else if (fName.endsWith(".pdf")) return "application/pdf";
else if (fName.endsWith(".htm") || fName.endsWith(".html") || fName.endsWith(".shtml")) return "text/html";
else if (fName.endsWith(".avi")) return "video/x-msvideo";
else if (fName.endsWith(".mov") || fName.endsWith(".qt")) return "video/quicktime";
else if (fName.endsWith(".mpg") || fName.endsWith(".mpeg") || fName.endsWith(".mpe")) return "video/mpeg";
else if (fName.endsWith(".zip")) return "application/zip";
else if (fName.endsWith(".tiff") || fName.endsWith(".tif")) return "image/tif";
else if (fName.endsWith(".rtf")) return "application/rtf";
else if (fName.endsWith(".mid") || fName.endsWith(".midi")) return "audio/x-midi";
else if (fName.endsWith(".xl") || fName.endsWith(".xls") || fName.endsWith(".xlv")
|| fName.endsWith(".xla") || fName.endsWith(".xlb") || fName.endsWith(".xlt")
|| fName.endsWith(".xlm") || fName.endsWith(".xlk")) return "application/excel";
else if (fName.endsWith(".doc") || fName.endsWith(".doc")) return "application/msword";
else if (fName.endsWith(".png")) return "image/png";
else if (fName.endsWith(".xml")) return "text/xml";
else if (fName.endsWith(".svg")) return "image/svg+xml";
else if (fName.endsWith(".mp3")) return "audio/mp3";
else if (fName.endsWith(".ogg")) return "audio/ogg";
else return "text/plain";
}
%>


Can anyone give me solution ?
Thanks in advance.....


SCJP5 and SCWCD1.5
Think Twice Act Wise...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

The problem is probably the line breaks at the top, after each JSP tag. Those line breaks are still sent event though the tags themselves aren't.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getOutputStream() has already been called for this response
 
Similar Threads
Tomcat showing error
Configuring Tomcat 5 on Win2K and Linux
Error messages in starting Tomcat 4.0.1 on Win2000
Apache2 / Tomcat 6 / GWT 2.0 - Could use some help
Downloading Excel file in jsp