• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with returning images from a servlet

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic