Re: Most efficient way to transfer a file directory recursively (using sockets)
"Arash Nikkar" <anikkar@gmail.com> wrote or quoted in
Message-ID: <1164154842.801238.263130@f16g2000cwb.googlegroups.com>:
I am trying to build a simple application which can be used much like a
ftp application, where the user will drag files over, and it will be
uploaded to a server.
To my thinking ....
<code>
//
// Main routine
//
OutputStream ou = ... // from socket.
File[] fs = getAllSendingFiles();
constructHead(ou, fs);
constructBody(ou, fs);
ou.flush();
// close stream and socket
....
//
// functions
//
File[] getAllSendingFiles() {
// Maybe recursive is needed.
return ...;
}
void constructHead(OutputStream out, File[] fs) throws IOException {
StringBuilder head = new StringBuilder(8196);
head.append("Protocol: Yours 1.0");
head.append(CRLF);
head.append("-"); // divider 1
head.append(CRLF);
for (File f : fs) {
addFileHead(head, f);
}
head.append("---"); // divider 3
head.append(CRLF);
out.write(head.toString().getBytes("utf-8"));
}
void addFileHead(StringBuilder head, File f) {
head.append("Path: " + f.getPath());
head.append(CRLF);
head.append("Length: " + f.length());
head.append(CRLF);
// head.append("Type: Binary"); // or Text/UTF-8
// head.append(CRLF);
head.append("--"); // divider 2
head.append(CRLF);
}
void constructBody(OutputStream ou, File[] fs) throws IOException {
for (File f : fs) {
ou.write(getFileBytes(f));
}
}
byte[] getFileBytes(File f) throws IOException {
// from FileInputStream..
return ...;
}
</code>