This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getOutputStream() has already been called for this response  RSS feed

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!