This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Error in Dowlnoading  RSS feed

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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.
 
Sheriff
Posts: 23692
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Sheriff
Posts: 23692
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!