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

P

Phil Hanna

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.
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top