internet file size?

M

Marco Schmidt

Disintegration:
Is there a way to know a file size through a java internet program? Such as
first reading the size of an image over the internet on a web page given
that image link.

Some webservers return a content-length HTTP header. But you can't
count on it being there.

Regards,
Marco
 
D

Disintegration

Is there a way to know a file size through a java internet program? Such as
first reading the size of an image over the internet on a web page given
that image link.

thanks
ray
 
J

Jon A. Cruz

Disintegration said:
Is there a way to know a file size through a java internet program? Such as
first reading the size of an image over the internet on a web page given
that image link.


You mean "over HTTP"?

Nope.

Servers are supposed to send the size in the header, but for 1.0 that
was purely advisory (aka "don't count on this being real").

However...

If you have a nice, well behaved server, you could send it an HTTP HEAD
request to see what it wants to tell you the size is. It's not 'know'
with the certancy of java.io.File.length(), but is usable as long as you
don't trust it 100% (i.e. don't statically allocate an array to that
size and trust you can shove in without checking)
 
M

Miguel De Anda

Disintegration said:
Is there a way to know a file size through a java internet program? Such as
first reading the size of an image over the internet on a web page given
that image link.

thanks
ray

I guess you can have the java program download the image then see how big it
was.
 
J

Jon A. Cruz

Phil said:
int length = con.getContentLength();
System.out.println("Image file size is " + length + " bytes");
}
}

You could fine tune this by opening a socket directly, issuing an HTTP
HEAD command, then scanning the response headers for
"Content-Length:".

Remember, especially for HTTP 1.0, that this lenght could be only "advisory"
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top