Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with returning images from a servlet

 
Pete Neu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic