This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Error in Dowlnoading  RSS feed

 
vinoth ar
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...
 
Bauke Scholtz
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.
 
Paul Clapham
Sheriff
Posts: 22487
43
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.
 
Hendy Setyo Mulyo
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: 22487
43
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.
 
Edwin Stephens
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!