compare file size with online file size

Discussion in 'Java' started by tiewknvc9, Sep 30, 2006.

  1. tiewknvc9

    tiewknvc9 Guest

    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?
     
    tiewknvc9, Sep 30, 2006
    #1
    1. Advertising

  2. 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'
    -------------------------------------

    Cheers,
    -Nick V.


    tiewknvc9 wrote:
    > 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?
     
    Nick Vatamaniuc, Sep 30, 2006
    #2
    1. Advertising

  3. 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.
     
    Andrew Thompson, Sep 30, 2006
    #3
  4. tiewknvc9

    tiewknvc9 Guest

    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){



    Andrew Thompson wrote:
    > 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.
     
    tiewknvc9, Sep 30, 2006
    #4
  5. 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.
     
    Andrew Thompson, Sep 30, 2006
    #5
  6. tiewknvc9

    Simon Brooke Guest

    in message <>,
    tiewknvc9 ('') wrote:

    > 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.

    --
    (Simon Brooke) http://www.jasmine.org.uk/~simon/
    Wannabe a Web designer?
    <URL:http://userfriendly.org/cartoons/archives/97dec/19971206.html>
     
    Simon Brooke, Oct 1, 2006
    #6
  7. tiewknvc9

    Chris Uppal Guest

    Simon Brooke wrote:

    > 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)
     
    Chris Uppal, Oct 1, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nani
    Replies:
    0
    Views:
    417
  2. chinni
    Replies:
    0
    Views:
    413
    chinni
    Nov 20, 2009
  3. chinni
    Replies:
    0
    Views:
    467
    chinni
    Nov 20, 2009
  4. chinni
    Replies:
    0
    Views:
    482
    chinni
    Nov 20, 2009
  5. chinni
    Replies:
    0
    Views:
    453
    chinni
    Nov 20, 2009
Loading...

Share This Page