File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes how to download an image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to download an image " Watch "how to download an image " New topic

how to download an image

pravallika reddy

Joined: Feb 24, 2002
Posts: 10
i want to download images of any type from the server is unix based.i need help help in coding.what should i give as content type in the response of jsp.
i am using jsp.
i had given image/jpg or image.gif as content type.but it is giving error as no preview parameters.
help me..
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
JSP is optimized for text - images are binary. The best way to serve binary data is through a servlet where you have total control over the response and can set the content type and length.
Servlets are just as simple as JSP, after all, a JSP is turned into a servlet. There is no reason not to use a mix of JSP and servlets for your application.
David Yutzy
tumbleweed and gunslinger
Ranch Hand

Joined: Jun 29, 2001
Posts: 192
I've got an issue with this approach...
I have a JSP file that has the following (<> removed so you can see the syntax):
IMG SRC="/servlet/imageservlet?id=6"
The problem is when I refresh the page, sometimes the image appears, sometimes it doesn't...
The same code, pointing to a PHP file for image retrieval works just fine...
Any suggestion?
The "id=" is pulling the image from a mySQL DB given the "id" of the image table.
Dave Van Even
Ranch Hand

Joined: Jul 19, 2001
Posts: 101
just MAP the servlet to a file with the right extension..
also make shure you write the right response content type !!

and map the servlet to somthing like: /imageservlet.gif

that should work
I agree. Here's the link:
subject: how to download an image
It's not a secret anymore!