Getting illegalStateExcepiton ???
illegalStateExcepiton : getOutputStream has been already called or this response. This is a problem which I recently faced while building my application in spring. So whats causing this exception ? heres the piece of code used to download a file which caused the error :
// inside handle request iternal
if (request.getParameter("download_id") != null) {
int id = ServletRequestUtils.getRequiredIntParameter(request,
"download_id");
List lss = resourcesService.downloadResources(id);
resources = lss.get(0);
response.setContentType(resources.getType());
response.setContentLength(resources.getResource().length);
response.setHeader("Content-Disposition", "attachment; filename=\""
+ resources.getResourceName() + "\"");
FileCopyUtils.copy(resources.getResource(),
response.getOutputStream()); // exception thrown here
}
// other codes goes here
//finally return mav
the code appears to be fine and the download is completed successfully too..except for the exception.
the exception is thrown simply because once the if loop finishes the mav object is returned which causes the error..
hence the exception is thrown when we are trying to invoke the response again after the download is complete ie; to render the mav object..hence simply by "returning null" the exception can be fixed.
return null; inside the if loop solved the problem for me.
// inside handle request iternal
if (request.getParameter("download_id") != null) {
int id = ServletRequestUtils.getRequiredIntParameter(request,
"download_id");
List
resources = lss.get(0);
response.setContentType(resources.getType());
response.setContentLength(resources.getResource().length);
response.setHeader("Content-Disposition", "attachment; filename=\""
+ resources.getResourceName() + "\"");
FileCopyUtils.copy(resources.getResource(),
response.getOutputStream()); // exception thrown here
}
// other codes goes here
//finally return mav
the code appears to be fine and the download is completed successfully too..except for the exception.
the exception is thrown simply because once the if loop finishes the mav object is returned which causes the error..
hence the exception is thrown when we are trying to invoke the response again after the download is complete ie; to render the mav object..hence simply by "returning null" the exception can be fixed.
return null; inside the if loop solved the problem for me.
