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?
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 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.
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.