views:

1535

answers:

1

Is there some trick to getting a SpringMVC custom view to cause a file download in the broswer? I've implemented the render method from org.springframework.web.servlet.View but the code results in my data being written to the page as a blob of data rather having a download action start.

try {

    Document oDoc = (Document) model.get("oDoc");
    out = new PrintWriter(response.getOutputStream());

    response.setContentType("application/vnd.ms-excel");
    response.setHeader("content-disposition", "attachment; filename=file.xls");

    GenerateXLSFile gof = new GenerateXLSFile();

    gof.outputTSVFromDom(out, oDoc);

} catch block here {

  //writes to log here

} finally {

    if (out != null) {
        out.flush();
        out.close();
    }

}

I know the render method is being called from the server logs. I know the GenerateXLSFile is being created from the server logs. I know the outputTSVFromDom can take a document and transform it work from my JUnit test. It also writes to the server log and completes. The data ends up in the browser. The HTTP Headers looks normal according to firebug. No errors from the catch block are in the server log.

What am I missing here?

+4  A: 

First of all, which API are you using? Excel documents are binary, so you should use OutputStream, not Writer.

Second, Spring has a built in support for serving Excel documents:

Another place you may want to look at is http://www.zabada.com/tutorials/excel-generation-with-spring-and-poi.php

David Rabinowitz
it was easier to re-write this to use AbstractExcelView than it was to keep the clunky old tab-value code.
sal