Struts: findForward after flushing output stream.

H

Hikikomori

Hi all,

I am using Struts 1.2.4 and I have an Action that flushes a generated
excel file (Using Apache POI) to the client. The client is able to
download the file but an exception is thrown because the output stream
has been flushed. I am just wondering if there is a way to work around
this issue.

The code:

public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
.....
response.setContentType("application/x-download");
response.setHeader("Content-Disposition",
"attachment; filename=file.xls");

OutputStream out = response.getOutputStream();

// Generates the file
HSSFWorkbook wb = myService.makeExcel();

wb.write(out);
out.flush();
out.close();

// Can't forward after committing out stream. Exception thrown.
return mapping.findForward("forward.exportSuccess");
}
 
R

Ryan Dillon

Because you have dealt with the response yourself, there is no need to
(and in fact you can't as you have found out) return an ActionForward.
If you return null, that will let Struts know you have dealt with the
response and that there is nothing more to do.

Cheers
 
T

Thomas 'Skip' Hollowell

Ryan said:
Because you have dealt with the response yourself, there is no need to
(and in fact you can't as you have found out) return an ActionForward.
If you return null, that will let Struts know you have dealt with the
response and that there is nothing more to do.

Ryan,

I too am working this same concept. Any ideas how I would display a
new page after the download is complete? My users want to see how
many lines should be in the document they just downloaded, cause
they are throrough like that. If I can't findfoward to another page
after the export/download, I can't display this info to them.

Ideas or suggestions?

Skip.
 
R

Ryan Dillon

Hi Skip,

The problem is that you are handling just one request, so you can only
make one response, which is the download. If you really wanted to
forward them to a page *after* they have successfully downloaded it,
you would need to do some tricks like creating a refreshing page that
kept polling the server to see how their download was going, and then
having it redirect when the server says it is done. But for what you
describe, if this information is available *before* then download (and
it sounds like it should be), then why not just tell them before they
download it, ie on the page with the download link?

Cheers
Ryan
 
T

Thomas 'Skip' Hollowell

Ryan Dillon wrote:
then why not just tell them before they
download it, ie on the page with the download link?

I didn't even think of pre-telling them. It seemed counter-intuitive to me,
but lo and behold, the customer really likes the idea. Thanks a million.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top