Bishu
Bishu

Reputation: 31

Java Servlet Download muptiple csv files

I have a report which displays some information in a report and I have link in the report to export to CSV. To download the csv file what we are doing is ,

public class ReportServlet extends XYXServlet{

        public void service(HttpServletRequest req, HttpServletResponse res) throws Exception {
        ...................
        ...................
        res.setContentType("text/csv");
        res.setHeader("Content-Disposition","attachment; filename=\""+reportName+"\"");
        OutputStream out = res.getOutputStream();
        // Render the report
        ReportRender.renderReport(report,results,out,rtParam);
        out.close();

}
}

This report is for one patient. Now I have the requirement where I have to download report for all the patient in the system. We have more than 5000 patients. It is a one time download. SO basically I should have one CSV file per patient .eg filename will be xyzreport-patientId. We are using velocity template . Basically ReportRender will take the report result and merge with the template using velocity template. like

VelocityContext c = new VelocityContext(params);
    Writer w = new OutputStreamWriter(out);
    template.merge(c,w);
        w.flush();

So now my problem is how do I download all report for all patients at one time. Can I use one request/response to download reports for all patients?

Upvotes: 0

Views: 68

Answers (1)

user3657302
user3657302

Reputation: 347

You can use zip file creation.

Best Practices to Create and Download a huge ZIP (from several BLOBs) in a WebApp

In above example they have BLOBs to download. In your case you need to write CSV files on zipped stream. If you will process all at a time and then sending them will cause memory issue. You need to do it loop; writing on stream as soon as you read it. This will increase efficiency of output as well as will avoid memory issues.

Above question has also answer along with implementation which is submitted by one who asked question. It is tried and tested. :)

Upvotes: 1

Related Questions