Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is this possible? <img src="/ImgMgr?name=imgName"> 1

Status
Not open for further replies.

Kindon

Programmer
Apr 19, 2001
54
0
0
US
We are trying to create an image manager servlet. We pass it information and it returns the image that we need. Not the text to get the image, but the byte stream of the image. Since the browser is expecting a byte stream of image data, we have set the servlet content type to "image/jpg". However, this is not working.

This is how we originally planned to use it:
<img src=&quot;/ImgMgr?name=imgName&quot;>

We realized that the browser was expecting some extension. So we added JavaScript:

<img id=&quot;documentImage&quot; src=&quot;/images/blankImage.jpg&quot;>
<script language=&quot;JavaScript&quot;>
document.getElementById(&quot;documentImage&quot;).src = &quot;/ImgMgr?name=imgName&quot; + &quot;.jpg&quot;;
</script>

This all sounded good until we realized that the &quot;.jpg&quot; is added before making the request to the server for the image.

We have come to a dead halt. Any suggestions?
 
>>> We are trying to create an image manager servlet. We pass it information and it returns the image that we need


Are you creating images on the fly - ie creating an image and using an OutputStream (response.getOutputStream()) to write byte data straight to the browser, or are you trying to create an image in the servlet, and then writing it to the server's disk, and then using <img src> to pick up that image ?
 
We are trying to use OutputStream (response.getOutputStream()) to write byte data straight to the browser
 
Then you need to do something like :

Code:
OutputStream out = response.getOutputStream();

// create a byte[] array of your image somehow
byte[] img  = getImageBytes();

out.write(img);
out.flush();
 
You also must set the content-type header to something like &quot;img/gif&quot; or whatever you are creating&quot;.

-pete
 
OK, Sedj. We tried that. for some reason we are getting an error on out.write(img).

I also found that the browser reads the image type from the image header. This means we do not need to bother with adding the extension or any JavaScript. :)

After debugging the error, we should be on our way. Thanks for your help!
 
As it turns out, we were getting our images from the database. The image was corrupt so it was generating an exception when we tried to write. This was probably our original problem. :(

Thanks for your input....
 
Hi,

I think, you might need to use some thing like this

FileOutputStream os = new FileOutputStream(fileName);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
JPEGEncodeParam encodeParam = JPEGCodec.getDefaultJPEGEncodeParam(bi);
encodeParam.setQuality(1f, true);
encoder.encode(bi);
os.flush();
os.close();

This could help you.

Cheers,
venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top