Spring -mvc download action

Is there some trick for getting a custom SpringMVC view to trigger the file upload in the browser? I implemented the method render

from org.springframework.web.servlet.View

, but the code causes my data to be written to the page as a drop of data, and from the moment the load starts.

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 called from the server logs. I know GenerateXLSFile is generated from server logs. I know that outputTSVFromDom can take a document and transform it from my JUnit test. It is also written to the server log and ends. The data ends up in the browser. The HTTP headers look fine according to firebug. There are no errors in the catch block in the log block.

What am I missing here?

+2


source to share


1 answer


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

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



+5


source







All Articles