How to determine size of a remote file?
Hi,
I have a question - is it possible to create a java.io.File object with remote files to determine files' size? For example, if I want to do something like:
java.io.File remoteFile = new java.io.File("http://www.filecart.com/file.zip");
long size = remoteFile.size();
Is this possible?
or if this impossible, how to determine the size of a remote file?
and is there a way to set some multithread downloading and download not from a beginning, but from some part of the file?
Regards and thanks in adwance,
M.
> java.io.File remoteFile = new java.io.File("http://www.filecart.com/file.zip");
> Is this possible?
No, it's impossible.
> or if this impossible, how to determine the size of a remote file?
How are you going to access remote file? Each method requires own actions.
> and is there a way to set some multithread downloading and download not from a beginning, but from some part of the file?
There is way. But before - answer question above.
Now I access a file using a HTTPConnection and URL objects. Also I can use socket connections, for now I do not know what is better. Any way as a result I receive an InputStream and write it to local file. That's it. Sometimes I use connections via proxy, so I suppose it it better to use Sockets, but now as I told, I use HTTPConnection to establish a connection with a remote file to download.
So, please tell me if it is possible to determine a file's size before download. And to continue interrupted download.
Also I know , there is a method in the HTTPConnection class to receive URL's size. Does it work for download files?
Regards and thanks,
F.
Did you read javadoc for HTTPConnection and super classes? There is very intresting method called getContentLength(). If this method is unsuccess then there is no way to detect content size and you should just read as many data as possible.
As for partial content - read HTTP specification especially part about "range". I'm not sure this functionality implemented in HTTPConnection class - do small research by youself. If no - you'll need to use sockets.
I dont think that there is anything to be implemented in URLConnection to handle the partial downloads.
Normaly what you do is you send the server the range of bytes that you want in a request header. If the server support partial downloads it will respond with only that part of the file. In the response header there will be aheader indicating the range that is send with the response.
If the server does not support the partial download it will just send the entiar file.
LRMKa at 2007-7-14 22:45:45 >

Ok, thanks. Can you please show an example of a header to be sent? As I suppose, this is HTTP header and it can be sent via socket connection.So, can you please show 1 header here? Till now I used only basic HTTP requests like GET/POST nothing more.Thanks,F.
If you look at section 14.16 of the HTTP RFC specification, it tells about Content-Range headers. Here's an excerpt:
The Content-Range entity-header is sent with a partial entity-body to specify where in the full entity-body the partial body should be applied. Range units are defined in section 3.12.
Content-Range = "Content-Range" ":" content-range-spec
content-range-spec= byte-content-range-spec
byte-content-range-spec = bytes-unit SP
byte-range-resp-spec "/"
( instance-length | "*" )
byte-range-resp-spec = (first-byte-pos "-" last-byte-pos)
| "*"
instance-length= 1*DIGIT
The full RFC is here:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.16