0

I try to change this one-file download to multi-files download one after one, not in zip. Every time I use this code, it downloads only first file (but the loop continues). I think it's because of httpServletResponse.setContentType(mimeType);

I've tried to solve this in some other ways, but nothing has worked.

@Override
    public void handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {

        List<Example> examples = exampleService.findAll();

        for (Example example : examples) {

            try {

                    Blob blob = new SerialBlob(example.getFileContent());
                    InputStream inputStream = blob.getBinaryStream();
                    int fileLength = inputStream.available();

                    String mimeType = example.getContentType();
                    if (mimeType == null) {
                        mimeType = "application/octet-stream";
                    }

                    httpServletResponse.setContentType(mimeType);
                    httpServletResponse.setContentLength(fileLength);

                    String headerKey = "Content-Disposition";
                    String headerValue = String.format("attachment; filename=\"%s\"", example.getFileName());
                    httpServletResponse.setHeader(headerKey, headerValue);

                    OutputStream outputStream = httpServletResponse.getOutputStream();

                    byte[] buffer = new byte[4096];
                    int bytesRead = -1;

                    while ((bytesRead = inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, bytesRead);
                    }
                    inputStream.close();
                    outputStream.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
Helosze
  • 333
  • 2
  • 7
  • 20

0 Answers0