1

Is there any way in Spring to provide a File download and a view? I want to give the user an automatic download and inform the user with a new html page.

 @RequestMapping(value = "/abgeschlossen/{bildcodes}")
 @ResponseBody
  public byte[]  download(@PathVariable List<String> bildcodes,
  HttpServletRequest request, HttpServletResponse response) throws 
IOException, ServletException {

    response.setContentType("application/zip");
    response.setStatus(HttpServletResponse.SC_OK);
    response.addHeader("Content-Disposition", "attachment; filename=\"test.zip\"");

    //creating byteArray stream, make it bufforable and passing this buffor to ZipOutputStream
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
    ZipOutputStream zipOutputStream = new ZipOutputStream(bufferedOutputStream);


    //packing files
    for (int i = 0;i<bildcodes.size();i++){
        Bild a = bildDao.findBildByCode(bildcodes.get(i));
        if (a!=null) {
            //new zip entry and copying inputstream with file to zipOutputStream, after all closing streams
            zipOutputStream.putNextEntry(new ZipEntry("Bild" + i + ".jpg"));
            ByteArrayInputStream fileInputStream = new ByteArrayInputStream(a.getDatei());

            IOUtils.copy(fileInputStream, zipOutputStream);

            fileInputStream.close();
            zipOutputStream.closeEntry();
        }
    }

    if (zipOutputStream != null) {
        zipOutputStream.finish();
        zipOutputStream.flush();
        IOUtils.closeQuietly(zipOutputStream);
    }

    IOUtils.closeQuietly(bufferedOutputStream);
    IOUtils.closeQuietly(byteArrayOutputStream);
    //RequestDispatcher rd=request.getRequestDispatcher("/abgeschlossen/fertig");
    //rd.forward(request, response);//method may be include or forward
    return byteArrayOutputStream.toByteArray();
}

I simultaneously need to return my view

return "finished.html";

I found some solutions with the HTMLresponse but there I have problems with to many redirects.

Hope someone can help me.

maxi
  • 11
  • 2

1 Answers1

0

You can add an AJAX call to the finished.html

OR

call AJAX download script and after start success redirect to finished.html

See Handle file download from ajax post

Community
  • 1
  • 1
StanislavL
  • 56,971
  • 9
  • 68
  • 98