0

I have controller method that starts downloading file once user click "Download" button. List of Objects can contain large amount of objects up to 400 000. When I put in csv file small amount of objects - it works fine .

@RequestMapping(value = "/downloadCSV")	public void downloadCSV(HttpServletResponse response) throws IOException	{	response.setContentType("text/csv");	String reportName = "metrics.csv";	response.setHeader("Content-disposition", "attachment;filename="+reportName);	ArrayList<String> rows = new ArrayList<String>();	rows.add("A, B, C, D, E");	rows.add("\n");	for (int i = 0; i < someList.size(); i++) {	String row = metricsDownloadModelList.get(i).getContentId()	+ someList.get(i).getA() + ", "	+ someList.get(i).getB() + ", "	+ someList.get(i).getC() + ", "	+ someList.get(i).getD() + ", "	+ someList.get(i).getE()	+ "\n";	rows.add(row);	}	Iterator<String> iter = rows.iterator();	while (iter.hasNext()) {	String outputString = (String) iter.next();	response.getOutputStream().print(outputString);	}	response.getOutputStream().flush();	}

But when I'm increasing the number of object in list to 1000 or more, it . give me an error:

Error executing a controller { org.apache.coyote.http11.HeadersTooLargeException: An attempt was made to write more data to the response headers than there was room available in the buffer. Increase maxHttpHeaderSize on the connector or write less data into the response headers.

Can I solve my problem without modifying my servlet config file? Is there any other approach to solve this problem?

6
  • yuo should increase the header size in your servlet container. In tomcat this is done in the server.xml file Commented Aug 31, 2017 at 16:32
  • @AngeloImmediata will it affect some other part of my team program? I am afraid to change configuration. What value should I put to maxHttpHeaderSize Commented Aug 31, 2017 at 16:40
  • it should change nothing to the team. This property simply tell the servlet container to increase the header dimension. About he value it depends on your scenario. I guess that 5MB would be enough Commented Aug 31, 2017 at 17:06
  • @AngeloImmediata . I just changed to 5MB and reload my server. But still get same error. Is there anything I should check or do fix problem? Commented Aug 31, 2017 at 17:08
  • It's strange also because I can download a huge file without any secial configuration. But what I saw is that you are "building" the csv file manually. Why? It's pretty non common Commented Aug 31, 2017 at 17:13

1 Answer 1

1

In order to download the file I would do the following:

  • create a temp CSV file
  • download the file

In order to do it I would write this kind of controller:

@RequestMapping(method = { RequestMethod.GET }, value = { "/downloadCsv" }) public ResponseEntity<InputStreamResource> downloadCSV() { try { String fileName = "test.csv"; //Create here your CSV file File theCsv = new File(fileName); HttpHeaders respHeaders = new HttpHeaders(); MediaType mediaType = new MediaType("text","csv"); respHeaders.setContentType(mediaType); respHeaders.setContentDispositionFormData("attachment", fileName); InputStreamResource isr = new InputStreamResource(new FileInputStream(theCsv)); return new ResponseEntity<InputStreamResource>(isr, respHeaders, HttpStatus.OK); } catch (Exception e) { String messagge = "Error in CSV creation; "+e.getMessage(); logger.error(messagge, e); return new ResponseEntity<InputStreamResource>(HttpStatus.INTERNAL_SERVER_ERROR); } } 

By using this kind of controller I'm able in downloading very huge files

Angelo

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

7 Comments

This code does not compile due to the bracket above catch clause.
Also it throws an exception as file path not found .
well this depends on your own context; i can't solve it. sure you must adapt code to your context
It does not have anything to do with the constext . If the file does not exist, well you get eat the fileNot found Exception because the inputsream reads from an existing file. Please add if(!theCsv.exists()) { theCsv.createNewFile(); // create your file on the file system }
Sorry for the tone of my comment if you find it a bit harsh :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.