5

I am exercising a simple application with HTML client that uploads a file to the server and must display a progress bar :)

The server is servlet-based, some servlet receives the multipart content, parses it etc. Obviously, at every moment I know the number of bytes received so far. However in order to supply an info for the progress bar on the client side a servlet also needs the total number of bytes for the file, which is being uploaded. Any idea where to get the total number of bytes from please?

Thanks!

BreakPhreak
  • 10,940
  • 26
  • 72
  • 108

2 Answers2

2

I think you have to implement the progress bar in javascript on the client side.

Anyway, this link gives an explanation of how to do it using JQuery.


The server can obtain the size of the multipart being uploaded from the HTTP request header; see @ilya's answer. However, getting that information and the count(s) of bytes read so far into a stream of HTTP responses that display or update a progress bar would be rather complicated ...

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Sure, I'll use JS on the client side. Thanks for helping, though. – BreakPhreak May 07 '11 at 12:23
  • 1
    I think you missed my point. The progress needs to be **measured** on the client side. – Stephen C May 07 '11 at 13:47
  • Ah, thanks for clarifying. I think that for my code server-side measurements will be fine enough. – BreakPhreak May 07 '11 at 15:54
  • 1
    If you upload a file the content length is not the filesize! – redestructa Aug 31 '14 at 13:21
  • @redestructa - Yea ... but I never said it was. What the progress monitor does is to compare the content length with the number of bytes read from the stream. Neither of these involves the file size. – Stephen C Aug 31 '14 at 22:51
  • 1
    Now obviously if the OP (pedantically) wanted to show a progress monitor that excluded the non-file envelope, then yes he would have to measure the (per file) bytes transferred at a different level. But frankly, the end-user wouldn't be able to tell the difference, so that would be wasted coding effort. – Stephen C Sep 01 '14 at 00:26
0

Size of content are placed in Content-Length header (see rfc2616). In Servlet you can get this header's value by request.getContentLength() or reqest.getHeaders("Content-Length")

ilalex
  • 3,018
  • 2
  • 24
  • 37
  • Thanks, just one question along: if I request for the complete Content-Length (and there is only one file being uploaded), does the length also count the other textual form fields? – BreakPhreak May 07 '11 at 12:12
  • 2
    Yes. Content-Length includes size of other fields. I recomment you to use Apache File Upload lib for parsing Http Request. – ilalex May 07 '11 at 12:30
  • 7
    Thus this answer is really not technically correct ... the HTTP header (there is only one on the POST request) "Content-Length" is optional AND moreover, includes all of the form-fields, overhead bytes, etc PLUS the actual size of the FILE part (which is what the OP asked about). In short, the HTTP Header "Content-Length" is NOT equal to the size of the uploaded file. Sadly the right answer is terribly complex. – Darrell Teague May 19 '16 at 22:22