compare file size with online file size

T

tiewknvc9

hi!

Im trying to make something that compares 2 files. One of the files is
on the users computer....

The other is on a website!

Obviously what Im trying to do is compare the lastModified date, in
order to update the file on the users computer if neccessary.

Only problem is that I dont know how I can get the size of the file
online without downloading it first...

Any ideas?
 
N

Nick Vatamaniuc

Would the HTTP "Content-length" header line help you? Some servers
don't send that so sometime you wouldn't know ahead of time. But in
general you can use the urllib module and then the info() method of the
urllib.urlopen() handler.

Here is the magic:

------------------------------------
In [1]: import urllib

In [2]:
hand=urllib.urlopen('http://www.python.org/images/python-logo.gif')

In [3]: hand.info().dict['content-length']
Out[3]: '2549'
 
A

Andrew Thompson

tiewknvc9 wrote:
...
....in
order to update the file on the users computer if neccessary.

WebStart can handle that (for data files, as well as classes)
for the user automatically and relatively painlessly - with a
progress dialog.

Andrew T.
 
T

tiewknvc9

unfortunately Im trying to avoid webstart (I dont like the whole code
signing thing)

So I created a class... something like this to compare the file sizes

//get online file size
URL url;
URLConnection conn;
long lSizeOfOnlineFile = 0;
long lSizeClientFile = filClientFile.length();
System.out.println("\nSize of Client file: " + lSizeClientFile + "
" + filClientFile.getAbsolutePath());
try {
url = new URL(strRemoteFile);
conn = url.openConnection();
lSizeOfOnlineFile = conn.getContentLength();
if(lSizeOfOnlineFile < 0){
System.out.println("Could not determine file size.");
}else{
System.out.println("\nSize of Online file: " +
lSizeOfOnlineFile);
}
conn.getInputStream().close();
}
catch(Exception e) {
e.printStackTrace();
}

//compare and act if files modification date are different
if (filClientFile == null){
System.out.println("filClientFile = null");
}

if (lSizeOfOnlineFile != lSizeClientFile){
 
A

Andrew Thompson

tiewknvc9 wrote:

Please refrain from top-posting.
unfortunately Im trying to avoid webstart (I dont like the whole code
signing thing)

WebStart applications do *not* need to be signed if ..
- they don't require a 'trusted environment'
- (or) they use the inbuilt JNLP API to access things
like files, clipboard and printer.

Re engineering a normal 'full access' application to
work within the JNLP API's can be tricky, but is
worth investigating.

Andrew T.
 
S

Simon Brooke

hi!

Im trying to make something that compares 2 files. One of the files is
on the users computer....

The other is on a website!

Obviously what Im trying to do is compare the lastModified date, in
order to update the file on the users computer if neccessary.

Only problem is that I dont know how I can get the size of the file
online without downloading it first...

Any ideas?

HTTP head request will get you the size and last changed date without
pulling the whole file.
 
C

Chris Uppal

Simon said:
HTTP head request will get you the size and last changed date without
pulling the whole file.

And sometimes the server actually obeys the HTTP spec and sends back the
correct information...

-- chris

(Actually, they often do -- but it's not something to rely on)
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top