0

Below is the piece of code that causes OutOfMemory issue when I run my xyz.war in tomcat 7 with Java 8.

In Below code I am creating a CSV response of the data that was fetched from MongoDB via cursor.

@RequestMapping(value = "/elements/{elementname}/records", method = RequestMethod.GET) public ModelAndView getAllRecords( HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "customerid", required = true) long customerId, @RequestParam(value = "userid", required = true) long userId, @RequestParam(value = "query", required = false) String query, throws Exception { Map < String, Object > model = new HashMap < String, Object > (); JsonObject records = elementService.searchRecords(query); ModelAndViewData msvd = elementService.commonRestService .getModelAndView("dataObject", "streamingView"); return new ModelAndView(msvd.getViewName(), handleCsvReportTypeRequest(records, customerId, userId)); } public Map < String, Object > handleCsvReportTypeRequest(JsonObject records, String elementName, long customerId, long userId) throws Exception { StringBuffer csvData = new StringBuffer(); // create csv data ModelAndViewData modelAndStreamingViewData = commonRestService.getModelAndView( "dataObject", "streamingView"); byte[] byteArray = String.valueOf(csvData).getBytes(); InputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); model.put(modelAndStreamingViewData.getModelAttributeName(), byteArrayInputStream); model.put(DownloadConstants.CONTENT_TYPE, DownloadConstants.CSV_CONTENT_TYPE); model.put(DownloadConstants.FILENAME, "XYZ.csv"); model.put(DownloadConstants.LAST_MODIFIED, new Date(System.currentTimeMillis())); model.put(DownloadConstants.CONTENT_LENGTH, Integer.valueOf(byteArray.length)); return model; } 

How can I stream CSV data back to the user without creating a huge data in memory and then passing to the user?

1 Answer 1

0

Use a Buffered read and write the response in HttpResponse object.

Try this way: Spring MVC : large files for download, OutOfMemoryException

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.