Getoutputstream() Has Already Been Called for This Response

getOutputStream() has already been called for this response

Ok, you should be using a servlet not a JSP but if you really need to... add this directive at the top of your page:

<%@ page trimDirectiveWhitespaces="true" %>

Or in the jsp-config section your web.xml

<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>

Also flush/close the OutputStream and return when done.

dataOutput.flush();
dataOutput.close();
return;

getOutputStream() has already been called for this response

You start by writing a response by yourself, and then return the name of the view, and thus telling Spring that it should generate an HTML page using the generate.jsp.

Make your method return void instead of a String. An HTTP response can have only one body: either it's a file attachment, or an HTML page, but not both.

IllegalStateException: getOutputStream() has already been called for this response on retrieving image

If you're just trying to send back the image data in response to a client image request, don't use a JSP. It doesn't really make sense because you're not returning any markup. Just write the image data straight to the output stream from the controller. Here's an example, refactored a little:

@RequestMapping("/catalogue/displayCatalogueImage.action")
public ResponseEntity getDisplayImage(String catalogueTitleNumber, HttpServletResponse response) throws Exception {

System.out.println("Image Display Title number = > "+catalogueTitleNumber);

if (StringUtils.isNotEmpty(catalogueTitleNumber)) {

response.setHeader("Content-Type", "image/png");
FileInputStream fin = new FileInputStream("images/"+catalogueTitleNumber);
ServletOutputStream out = response.getOutputStream();

IOUtils.copy(fin, out);
IOUtils.closeQuietly(fin);

response.flushBuffer();

return new ResponseEntity(HttpStatus.OK);
}

return new ResponseEntity(HttpStatus.NOT_FOUND);
}

getOutputStream() has already been called for this response Exception when downloading a file with spring mvc

Ditch the JSP, that code belongs in the controller and move the response.getOutputStream to a point where you need it.

@RequestMapping(value="/openfile", params ={"path"})
public void openFile(@RequestParam(value="path", required=false) String path, ModelMap model, HttpServletResponse response) throws ServletException {

try {
File file = new File(path);
response.setContentType("application/download");
response.addHeader("Content-Disposition", "attachment; filename=" + file.getName());
response.setContentLength((int) file.length());
FileInputStream input = new FileInputStream(file);
BufferedInputStream buf = new BufferedInputStream(input);
FileCopyUtils.copy(buf, response.getOutputStream());
} catch (IOException ioe) {
throw new ServletException(ioe.getMessage());
} catch (Exception ex) {
System.out.println("[openfile] Error: " + ex.getMessage());
}
}


Related Topics



Leave a reply



Submit