Discussions

Web tier: servlets, JSP, Web frameworks: Downloading a dynamically generated file

  1. Downloading a dynamically generated file (1 messages)

    I am developing a J2EE application, where we will dynamically generate files in response to user requests, and make them available for download. However, if I store the file in the server's filesystem, it does not help because all the jsps/htmls available for the client is kept in a docroot directory in the web archive (war) file. It is obviously not possible to regenerate the war file. How do I overcome this problem ? I donot want to send the file back in the response, but keep it somewhere so the user can go to that link and download it.

    Thanks in advance
  2. Use the following code[ Go to top ]

    Use the following code

    You need not save your file and send link.
    It will prompt the user to directly save the file...

    stream = response.getOutputStream();
    response.resetBuffer();
    response.setContentType("application/octet-stream");
    response.setHeader(
    "Content-Disposition",
    "attachment;filename=filenameyouwanttoshow.txt");
    response.setHeader("Content-Length", "-1");
    //stream.write your data
    stream.flush();