Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File Download through Servlet

 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it with

and still no luck
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,

Can you point me where the problem is in the code?
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally solved it

 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic