Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify image name in "on the fly" java image generation

I have some java code that generates an image from scratch.

I also have a servlet that serves the image according to some parameters (fyi, it is QR Code generation).

I would like to specify a name for this image in case somebody right-click and save it. THe browser currently takes the relative URL as a name (ie, generate), and there is no extension.

Do you have any idea ?

Thanks, Alexis.

like image 936
Alexis Laporte Avatar asked Mar 23 '26 19:03

Alexis Laporte


2 Answers

Use a nice url to encode your image.
I suppose now you're doing something like

generateServlet?product=27&query=5

instead, map it to

images/qr/product27_q5.png

This way, the browser will only see the nice url and propose that as a file name. It also hides your implementation more.

Just map your servlet to images/qr/* and then use a regular expression to extract the actual parameters from the requested path.

You could even generate different image types depending on the extension provided :)

like image 140
Joeri Hendrickx Avatar answered Mar 25 '26 09:03

Joeri Hendrickx


    resp.setContentType("image/jpg");
    resp.setHeader( "Content-Disposition", "attachment; filename=\"" + your_filename + "\"" );
like image 35
zawhtut Avatar answered Mar 25 '26 07:03

zawhtut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!