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.