If attachment file size is more than 16363byte Content-Disposition is not comming in response in Spring boot application.
I use Java 8 and Spring boot I need to send zip file with special file name.
HttpServletResponse response;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(byteArrayOutputStream);
zip.putNextEntry(new ZipEntry(foldername));
objectMapper.writerWithDefaultPrinter().writeValue(zip, list);
zip.closeEntry();
byteArrayOutputStream.writeTo(responseOutputStream);
ContentDisposition contentDisposition = ContentDisposition.type("attachment").fileName(fileName).build()
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
I don't want to use solution like File file = createTempCSVFile();
because I need always control attachment file.
Response headers must be sent before the response body. Spring and the servlet container try to buffer the output stream so that you can delay specifing the response headers, but - as you have noticed - this buffer is limited.
You can solve the problem easily by providing the response header before starting to write to the output stream:
ContentDisposition contentDisposition = ContentDisposition.type("attachment").fileName(fileName).build()
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(byteArrayOutputStream);
zip.putNextEntry(new ZipEntry(foldername));
objectMapper.writerWithDefaultPrinter().writeValue(zip, list);
zip.closeEntry();
byteArrayOutputStream.writeTo(responseOutputStream);