Prompting user for download in Servlet

D

Douglas Dillon

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
 
T

Tor Iver Wilhelmsen

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.
 
M

marcus

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");
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top