aspose file tools*
The moose likes Servlets and the fly likes Error in Dowlnoading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Error in Dowlnoading " Watch "Error in Dowlnoading " New topic
Author

Error in Dowlnoading

vinoth ar
Greenhorn

Joined: Jan 07, 2009
Posts: 21
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...


vino...
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You´re forgotten to set the response headers content-length and content-disposition.
vinoth ar
Greenhorn

Joined: Jan 07, 2009
Posts: 21
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

Joined: Oct 08, 2006
Posts: 2458
Uh. There is no why. The HTTP specification state so.
vinoth ar
Greenhorn

Joined: Jan 07, 2009
Posts: 21
hi Bauke Scholtz ,
i can`t get you. anyways thanks for your. kind reply.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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
Bartender

Joined: Oct 14, 2005
Posts: 18650
    
    8

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

Joined: Dec 01, 2004
Posts: 219

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;


Hendy Setyo Mulyo
SCJP 1.4 (95%), SCWCD 1.4 (94%)
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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
Bartender

Joined: Oct 14, 2005
Posts: 18650
    
    8

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

Joined: Dec 30, 2008
Posts: 15
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
 
 
subject: Error in Dowlnoading