aspose file tools*
The moose likes Servlets and the fly likes Problem with returning images from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with returning images from a servlet" Watch "Problem with returning images from a servlet" New topic
Author

Problem with returning images from a servlet

Pete Neu
Ranch Hand

Joined: Feb 18, 2005
Posts: 86
Hello,

I trying to return an image from a servlet as attachment. I know this question has been asked before but I couldn't find an answer on the forum.
The code to do this I copied from here: [ UD: removed link to copyrighted material ]



When I run the code I get no error. But when I open the attachmend dialogue I get the error that no file exists. The file is empty. I did some debugging but found no error. The picture has a size of 3k. The picture is in place under the path on the server.

Does anyone have an idea what's wrong?

cheers,
Pete
[ August 03, 2007: Message edited by: Ulf Dittmer ]
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Try flushing the output stream and closing it. It could be cached in the server and not sent.

[ May 15, 2006: Message edited by: Carl Trusiak ]

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There are a couple things here..

I'm guessing the problem is that you're using a new instance of ByteArrayOutputStream. You might want to use a ServletOutputStream obtained with response.getOutputStream.

Also, if your file is kept within your web app, you might want to consider using context.getResourceAsStream instead of new File to read the image file.
The former will work regardless of how your application is deployed.
The getRealPath method will return null if your app is deployed from a packed war file.

Here is the code from one of my example apps:


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with returning images from a servlet