0

I have the jai-imageio jar and have added it to my class path. I just don't know to write a .tif image to the response's output stream. Can someone help me?

Here is my code:

RenderedOp image = JAI.create("fileload", filepath);
ImageIO.write(image.getAsBufferdImage(), "tif", response.getOutputStream());

I know that javax.imageio.ImageIO doesn't support tif images, so what do I do with jai-imageio to make it worK? I'm lost.

Note: the code above works fine for other image types like jpeg and png.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
dmoss18
  • 867
  • 1
  • 12
  • 25
  • Why exactly do you need TIF image support if it works for you with PNG? – BalusC Oct 08 '12 at 20:00
  • Because users are uploading images, usually they are tif images, but we support tif, png, and jpeg and I am trying to stream their uploaded image back to them – dmoss18 Oct 08 '12 at 20:04
  • Oh? Are you manipulating uploaded images? (e.g. resize, chop, skew, rotate, etc)? Otherwise I really don't see any reason to use Java2D API this way to just stream the uploaded image back. – BalusC Oct 08 '12 at 20:07
  • What would you use to stream it back? – dmoss18 Oct 08 '12 at 20:08
  • Just read/write the bytes unmodified. – BalusC Oct 08 '12 at 20:09
  • Basically, I have an html , the src="url/to/image/streaming/action" Will streaming the bytes back break this functionality in any way? – dmoss18 Oct 08 '12 at 20:10

1 Answers1

2

It look like that you're going in the wrong direction as to storing and serving uploaded images. You don't need the whole Java 2D API for that at all.

When you retrieve an uploaded image, just do

InputStream input = uploadedFile.getInputStream();
OutputStream output = new FileOutputStream(uniqueImagePath);
// Now write input to output in a loop the usual way.

When you serve an uploaded image, just do

InputStream input = new FileInputStream(uniqueImagePath);
OutputStream output = response.getOutputStream();
// Now write input to output in a loop the usual way.

You don't need to massage/manipulate the bytes at all. Just stream them unmodified.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • If I have an html , will the image still show up using your approach? – dmoss18 Oct 08 '12 at 20:14
  • so I'm doing what you're suggesting, finishing with IOUtils.copy(input, output), but I'm getting a socketException: Connection reset by peer: socket write error. Do you know what that could mean? – dmoss18 Oct 08 '12 at 20:18
  • Client aborted the request. Can have many causes. E.g. client pressed Esc key, or client's son has cut network cable, or client machine caught fire by lightning, etc. Not server's problem. – BalusC Oct 08 '12 at 20:18
  • Here's an example for serving images from DB, you may find it helpful as well: http://stackoverflow.com/questions/2340406/how-to-retrieve-and-display-images-from-a-database-in-a-jsp-page/2341322#2341322 – BalusC Oct 08 '12 at 20:20
  • Your suggestions worked. Turns out some browsers simply cannot display tiff images, so I ended up converting them to jpegs before streaming. Accepting this answer. – dmoss18 Oct 11 '12 at 16:42