0

I am in a situation where on calling an API i want to download a excel file along with a zip file. I have cracked the code for downloading them separately, but when put together only one file gets downloaded and the other one just doesnt gets downloaded. I guss the problem is I cannot use response.getOutPutStream().flush() or response.flushBuffer() simultaneously.

String absolutePath = context.getRealPath("resources/ZipFolders");
String inputFile = Paths.get(absolutePath + "/Attachments.zip").toAbsolutePath().toString();
File finalFile = new File(inputFile);
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(finalFile));

String absolutePath2 = context.getRealPath("resources/Spreadsheets");
String inputFile2 = Paths.get(absolutePath2 + "/Validation_Spreadsheet.xlsx").toAbsolutePath().toString();
File file = new File(inputFile2);
byte[] bytes = IOUtils.toByteArray(new FileInputStream(file));
ZipEntry zipEntry = new ZipEntry("Validation_Spreadsheet.xlsx");
zipOut.putNextEntry(zipEntry);
zipOut.write(bytes);
zipOut.closeEntry();
zipOut.close();
response.setContentType("application/zip");
response.setHeader("Content-disposition", "attachment;filename=attachment_trial.zip");
response.getOutputStream().write(IOUtils.toByteArray(new FileInputStream(finalFile)));
System.err.println("above flush>>>>>>>>>>>>>>");
response.getOutputStream().flush();

responsetrial.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
responsetrial.setHeader("Content-disposition", "attachment; filename=TransactionErrors.xlsx");
responsetrial.getOutputStream().write(IOUtils.toByteArray(new FileInputStream(file)));
System.err.println("above flush2>>>>>>>>>>>>>>");
responsetrial.getOutputStream().flush();
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • Does this answer your question? [How to download multiple files with one HTTP request?](https://stackoverflow.com/questions/1041542/how-to-download-multiple-files-with-one-http-request) – Johnny Willer Nov 12 '19 at 15:06
  • i already read it doesnt silve the issue – BHAVESH VENU Nov 12 '19 at 16:12
  • You're trying to send 2 files over the same request/response, HTTP wasn't designed to this. One approach is to ZIP the two files and send that zipped file. Or design an approach with two requests/responses – Johnny Willer Nov 12 '19 at 16:19

1 Answers1

0

It's not possible to download 2 files over a single HTTP request. You will need to make 2 separately requests for this task.

If you need to download many files in a single "HTML button", you need to write some javascript logic two make this.

Johnny Willer
  • 3,717
  • 3
  • 27
  • 51