0

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.

Aliy
  • 197
  • 14
  • 1
    It looks like what you are trying to do doesn't make any sense. In the end you are making a HTTP call to a server, which will response with headers and a body. But you want to call a HTTP request which should return 4 PDF documents (which are saved/handled how?) AND a `ResultDto` object? What are you trying to accomplish? And btw.: `out.close();` will close the stream you are trying to use. – Progman Sep 11 '17 at 10:12
  • @Progman, can you advice me how to acheive this then? I want to have both concepts. Return an object and flush the docs too – Aliy Sep 11 '17 at 10:14
  • When you open your URL "`http://your_server/api/getDocs?tkey=abc`" in your browser, what should happen from the browsers point of view? – Progman Sep 11 '17 at 10:17
  • @Progman, I will update the total details again. Please check in 2 min – Aliy Sep 11 '17 at 10:17
  • @Progman, I have updated the question. Please check it – Aliy Sep 11 '17 at 10:20
  • The question still remains: What should happen from the browsers point of view? I enter the URL in my browser an hit enter, what do I see now? Should I see a pdf document? Should I see all pdf documents somehow? Should I see some JSON data of your `ResultDto` object? Should I see some html page saing "generation complete, download the four files here: (link1), (link2), (link3), (link4)"? Should I see a download of a zip file which contains the `ResultDto` data as well as the four pdf documents? – Progman Sep 11 '17 at 10:26
  • 2
    Maybe you want to take a look at https://stackoverflow.com/questions/2339440/download-multiple-files-with-a-single-action which describes how to have a browser download multiple items from a single action. – Roger Lindsjö Sep 11 '17 at 10:28
  • When you just hit enter, it should do both things. 1. Return me the ResultDto object(Should be able to see under network tab as response) and download the 4 documents. Its not required to show the pdf in browser. It will downloaded automatically in the downloads folder. – Aliy Sep 11 '17 at 10:29

0 Answers0