aspose file tools*
The moose likes Servlets and the fly likes File Download through Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "File Download through Servlet " Watch "File Download through Servlet " New topic
Author

File Download through Servlet

Mary Cole
Ranch Hand

Joined: Dec 02, 2000
Posts: 362
Hi,

I have a servlet which gets the file contents from an external URL and on click of a link from the browser, the file dialogue should open up prompting the user to save or open the file. Attached is the snippet code.
The problem am having is that even thou the code contacts the url and gets the file contents, when I write it to the outputstream, only part of the file gets written and as a result of that when I clcik on the open button on the file open dialogue box, the file doesn't open up properly because its only a bit of the whole file.

Can you please suggest where am wrong?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18124
    
    8

Your problem is here:

If is.read(block) returns zero, then you break out of the loop and don't send any data after that. But it's -1 which signals the end of the file, not 0. Apart from that the code looks fine.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
Also, I don't think you understand what InputStream.available() provides you. Look this up in the API docs.
Hint: It is not necessarily the entire length of the stream

I'm guessing this is your actual problem, but then I'm not a Bartender

Mary Cole
Ranch Hand

Joined: Dec 02, 2000
Posts: 362
I tried it with

and still no luck
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Mark E Hansen wrote:I'm guessing this is your actual problem, but then I'm not a Bartender

What does that have to do with anything?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
Bear Bibeault wrote:
Mark E Hansen wrote:I'm guessing this is your actual problem, but then I'm not a Bartender

What does that have to do with anything?

I was making a half-hearted joke that Paul's response, although correct, would not actually solve the OP's problem.
... perhaps it was too dry?
Mary Cole
Ranch Hand

Joined: Dec 02, 2000
Posts: 362
Mark,

Can you point me where the problem is in the code?
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
Mary Cole wrote:Mark,

Can you point me where the problem is in the code?


You're using is.available() (at line 25) to tell you how many bytes you need to send to the client. But that's not what the method returns. Read the Javadoc API for InputStream.available() to see what it actually does.

My guess is that this results in setting your contentLength variable to a value smaller than the actual file size.

Mary Cole
Ranch Hand

Joined: Dec 02, 2000
Posts: 362
Finally solved it

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18124
    
    8

Mark E Hansen wrote:Also, I don't think you understand what InputStream.available() provides you. Look this up in the API docs.
Hint: It is not necessarily the entire length of the stream

I'm guessing this is your actual problem, but then I'm not a Bartender



Yup, I missed that. Yes, there's almost never a good reason to use the available() method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Download through Servlet
 
Similar Threads
Corrupt file download
Session in HTTPServlet
Multiple Actions issue in JSF
Apache HttpClient posing trouble
File Download problem using icefaces