• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

File Download through Servlet

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Sheriff
Posts: 27465
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried it with

and still no luck
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally solved it

 
Paul Clapham
Sheriff
Posts: 27465
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic