Prompting user for download in Servlet

Discussion in 'Java' started by Douglas Dillon, Apr 1, 2005.

  1. I've inherited a Servlet that produces a jpg and displays it in a
    browser. I've been asked to change it to have the output saved to a
    location specified by the Client.

    It's quite simple at the moment

    private void outputJPEG(BufferedImage field, HttpServletResponse res,
    ServletOutputStream os)
    throws IOException
    {
    res.setContentType("image/jpeg");
    ImageIO.write(field, "jpeg", os);
    }

    Is there a simple way to prompt the user for a file and pipe the
    BufferedImage to it?

    Thanks

    Douglas
    Douglas Dillon, Apr 1, 2005
    #1
    1. Advertising

  2. (Douglas Dillon) writes:

    > Is there a simple way to prompt the user for a file and pipe the
    > BufferedImage to it?


    No that is 100% the browser/client's decision. Tell the users to
    change the browser behaviour if they want that.

    What you could do is use the "application/octet-stream", which most
    browsers by default will prompt a save dialog for. But also set a
    Content-Disposition header to "attachment;filename=whatever.jpeg" when
    you do.
    Tor Iver Wilhelmsen, Apr 1, 2005
    #2
    1. Advertising

  3. Douglas Dillon

    marcus Guest

    change the type of file to something the browser won't recognize --
    won't change the extension or data. from
    response.setContentType("image/jpeg");
    to
    response.setContentType("why/me");

    Tor Iver Wilhelmsen wrote:
    > (Douglas Dillon) writes:
    >
    >
    >>Is there a simple way to prompt the user for a file and pipe the
    >>BufferedImage to it?

    >
    >
    > No that is 100% the browser/client's decision. Tell the users to
    > change the browser behaviour if they want that.
    >
    > What you could do is use the "application/octet-stream", which most
    > browsers by default will prompt a save dialog for. But also set a
    > Content-Disposition header to "attachment;filename=whatever.jpeg" when
    > you do.
    marcus, Apr 2, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?a2lt?=
    Replies:
    3
    Views:
    7,557
    cbDevelopment
    Dec 15, 2005
  2. Replies:
    7
    Views:
    1,632
    Shunya
    Mar 1, 2006
  3. circuit_breaker
    Replies:
    2
    Views:
    1,981
    Jack Jia
    Apr 4, 2004
  4. Billy Zhang

    Print directly in IE without prompting user

    Billy Zhang, Aug 5, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    840
    Steven Cheng [MSFT]
    Aug 6, 2008
  5. Andrew Henry

    Prompting a download of an external file in ASP.

    Andrew Henry, Jul 20, 2005, in forum: ASP General
    Replies:
    3
    Views:
    160
    Bullschmidt
    Jul 21, 2005
Loading...

Share This Page