Redirect after loading pdf in spring

I am using spring boot. I wrote code to load data into a database. If something goes wrong, the data will be downloaded in pdf format, which is incorrect. I want to do this, I have to download this pdf file and go to another page.

@RequestMapping(value = "/doUpload", method = RequestMethod.POST)
public void doUpload(@ModelAttribute("employeeCsvForm") EmpFileUpload fileUpload,HttpServletRequest request, HttpServletResponse response){

    // codes for create pdf.

    if (errorCount != 0) { //errorCount is the count of error data  

        File fileEmp = new File("error.pdf");

        //downloading the pdf in browser path
        if (fileEmp.exists()) {
            FileUtils.copyFile(fileEmp, response.getOutputStream());
            response.setContentType("application/pdf");
            response.setHeader("Content-disposition", "attachment;filename=error.pdf");
            response.flushBuffer();             
        }   
    }
}

This code downloads the PDF file . But I need to redirect to another page, so I used

response.sendRedirect(request.getContextPath() + "/employee/errorCsv");after response.flushBuffer();Its loading pdf is successful , but the following error is displayed .

Error: getOutputStream () has already been called for this answer

When I write a redirect code response.sendRedirect(request.getContextPath() + "/employee/errorCsv");before response.flushBuffer();His direction to another page is successful, but it does not load .

I want to do both, I tried my best, but could not. Thanks in advance.

+4
1

.

, (, , Sourceforge), :

  • (, , )
  • Javascript ( )

, HTTP HTTP. HTTP , () - .

- - , , -. (.. ) -, . , :

, :

  • -
+3

Source: https://habr.com/ru/post/1690765/


All Articles