• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Error in Dowlnoading

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends..

here i tried to write program to download a jpeg image from folder Resource folder. it is downloading a jpeg image as ordinary file with 0 bytes. with name of file as dload(servlet name). i couln`t trace the logical could you correct me..








thank you...
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You´re forgotten to set the response headers content-length and content-disposition.
 
vinoth ar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Bauke Scholtz,

can you please explain me. why we need those functions. i`ve some knowledge about setcontentlen explght().. no knowledge about diposition.

can you explain for me.

thanks
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uh. There is no why. The HTTP specification state so.
 
vinoth ar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Bauke Scholtz ,
i can`t get you. anyways thanks for your. kind reply.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It´s just, say, the law of HTTP.

http://www.w3.org/Protocols/rfc2616/rfc2616.html

Asking "why so?" makes no sense. It´s just because it is definied so.
 
Marshal
Posts: 27275
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It also doesn't help that you read and discarded an unknown number of bytes from the JPG before your loop that tries to copy it to the response.
 
Ranch Hand
Posts: 219
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vinoth ar wrote:hi friends..

here i tried to write program to download a jpeg image from folder Resource folder. it is downloading a jpeg image as ordinary file with 0 bytes. with name of file as dload(servlet name). i couln`t trace the logical could you correct me..



First, you should make sure that the image file you want to download is in the correct path.

InputStream is = context.getResourceAsStream("/Resource/vino.jpeg");

In other words, you should check whether is != null;

 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have not checked the code closely, but all of above is ultimately correct. Your code is not robust enough.
You may find this example useful then: http://balusc.blogspot.com/2007/04/imageservlet.html

Besides, using POST for this makes no sense. Rather use GET. The <img src> always use GET. And no, you shouldn't call doPost() inside doGet().
 
Paul Clapham
Marshal
Posts: 27275
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In particular line #19 will read up to 46000 bytes of the resource into a byte array. You ignore this and just stream the rest of the resource out to the response.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Vinoth,

You can also use the ImageIO class to read the jpeg image file and write it to the response output stream, like this:



This technique is detailed here:
http://www.avajava.com/tutorials/lessons/how-do-i-return-an-image-from-a-servlet-using-imageio.html

Hope that helps,
Edwin
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic