Re: Copy a file over network using Java...

Discussion in 'Java' started by Phil Hanna, Jun 30, 2003.

  1. Phil Hanna

    Phil Hanna Guest

    On Sun, 22 Jun 2003 07:29:51 GMT, "tanis1" <>
    wrote:

    >What is the best way to stream a file over a network from one
    >host:/partition
    >to another host:/partition. It would be nice to preserve the file
    >owner:group
    >and permissions if such a user account existed on the other host.


    Two approaches:

    1. Simply copy the file as an ouput stream:

    Socket socket = new Socket(host, port);
    InputStream in = new BufferedInputStream(file);
    OutputStream out =
    new BufferedOutputStream(socket.getOutputStream());
    byte[] buffer = new byte[BUFFER_SIZE];
    while (true) {
    int nBytes = in.read(buffer, 0, BUFFER_SIZE);
    if (nBytes < 0)
    break;
    out.write(buffer, 0, nBytes);
    }
    out.flush();
    out.close();
    in.close();

    This works fine, but of course doesn't preserve file permissions, etc.

    2. Use classes in java.util.zip to create a zip archive that contains
    the file, transmit this zip file as shown in step 1, then unzip the
    file. Seems to me there are options that will allow you to preserve
    permissions. Same approach would work with java.util.jar.
    --
    Phil Hanna
    Author of JSP 2.0: The Complete Reference
    http://www.philhanna.com
    http://www.philhanna.com
    Phil Hanna, Jun 30, 2003
    #1
    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. Mark Goldin

    Copy files over the network

    Mark Goldin, Jul 8, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    480
    Arran Pearce
    Jul 8, 2004
  2. Jane Davis

    Network Service account over network

    Jane Davis, Jun 22, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    440
    Kevin Spencer
    Jun 22, 2005
  3. Replies:
    7
    Views:
    6,702
    Alex Molochnikov
    Feb 23, 2005
  4. Alex
    Replies:
    2
    Views:
    1,218
  5. Replies:
    26
    Views:
    2,108
    Roland Pibinger
    Sep 1, 2006
Loading...

Share This Page