I'm trying to generate documents in a sequence using ServletOutputStream. I've total of 4 documents. I've put these as a string array and then iterating the array to get each. But only first document is being flushed, but the other three are not. When I debug, it is executing all lines, but dont know why remaining three are not getting download. Here is my code
The flow is
when I point to "my_server/api/getDocs?tkey=abc", it will make one external web service call from the implementation. Upon that I will check whether I received OK status. If I get OK status I have to generate the 4 docs.
public ResultDto getDocs(HttpServletResponse response,@PathVariable("tKey") String tKey) throws ClassNotFoundException, IOException {
    logger.debug(tKey);
        ResultDto result = new ResultDto();     
        result = myService.confirmTransaction(tKey);
        if(QBConstants.OK.equals(result.getStatus())) {
            String[] documents = {"2","3","4","5"};// 
            for (String documentKey: documents) {           
                    byte[] documentBytes =  myService.generateDocument(tKey, documentKey);
                    String documentType = myUtil.getDocumentType(documentKey);
                    response.setHeader("Content-Disposition", "attachment;filename="+documentType);
                    response.setContentType("application/pdf");
                    response.setHeader("Expires", "0");
                    response.setHeader("Cache-Control", "must-revalidate, postcheck=0, pre-check=0");
                    response.setHeader("Pragma", "public");
                    response.setContentLength(documentBytes.length); 
                    ServletOutputStream out = response.getOutputStream();
                    out.write(documentBytes);
                    out.flush();
                    out.close();
                    logger.debug("********** DOCUMENT GENERATED **********");
            } 
          }
    }
    return result;      
And the return data is not able to receive. I want to flush as well as get the result object. Before flushing the documents logic, I was able to get the result object.
Any ideas would be greatly appreciated.
