Size

F

freesoft_2000

Hi everyone,

I have a really silly question but please bear with me
for a while.

If say i connect to the url http://www.yahoo.com/a.zip
using the URLConnection how do i get the size of the above file in bytes
assuming the connection is successfull?

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
 
R

Roedy Green

If say i connect to the url http://www.yahoo.com/a.zip
using the URLConnection how do i get the size of the above file in bytes
assuming the connection is successfull?

the problem is the server might not tell you or might not even know.
It can in theory deliver an infinite stream dynamically generated.

When it does know, see http://mindprod.com/applets/fileio.html

You can experiment to see if it will tell you the length with just a
HEAD request. You may have to issue the GET to find it out.

And tell it you want to read an URL. If you are feeling adventurous,
you can even do it at the raw socket level. Snoop on what a browser
does and copy it. see http://mindprod.com/jgloss/cgi.html
also see http://mindprod.com/jgloss/urlconnection.html
 
R

Roedy Green

I have a really silly question but please bear with me
for a while.

IF you think the question is "silly" i.e. novice level, then you
should ask it in comp.lang.java.help
 
R

Roland

Hi everyone,

I have a really silly question but please bear with me
for a while.

If say i connect to the url http://www.yahoo.com/a.zip
using the URLConnection how do i get the size of the above file in bytes
assuming the connection is successfull?

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West

yourURLConnection.getContentLength(), which may return -1 if the
webserver (in case of the http URL) doesn't return this information
(i.e. a HTTP Content-Length header field).

<http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLConnection.html#getContentLength()>
--
Regards,

Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \
 
F

freesoft_2000

Hi everyone,

Roland, i saw the documentation and i know you are talking
about
getContentLength(). The thing is that it returns an int. What if it is a
huge zip file, would not a long be better. Another thing is the length
format that it returns was not specified(ie. whether its in bytes,
kilobytes or megabytes)
that's why i am confused.

Do you know the format it returns?

Richard West
 
C

Chris Uppal

freesoft_2000 said:
getContentLength(). The thing is that it returns an int. What if it is a
huge zip file, would not a long be better.

I think it probably should be -- it's actually telling you how much data the
server /claims/ it will send. Arguably it should be a String since the
server may send a
Content-length: Snow White
response, which cannot be expressed as either an int or a long...

In practise it's the size in bytes of the content, but I warn you against
trusting it too much. It's fine for use as an indication of how big the
contents will be (e.g. for progress monitoring, or as an initial /assumption/
about buffer sizes), but you shouldn't make your application logic dependent on
it always being exactly correct. In far too many cases it is not.

-- chris
 
R

Roedy Green

getContentLength(). The thing is that it returns an int. What if it is a
huge zip file, would not a long be better. Another thing is the length
format that it returns was not specified(ie. whether its in bytes,
kilobytes or megabytes)
that's why i am confused.

the actual message contains the length as a string of decimal digits
as bytes.

The high level way to get it is
URLConnection.getLength which returns an int. It is measured in
bytes.

Understand that this field is optional. That is why you can download
a file bigger than 2 meg. You get 0 and just launch out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top