channel session io ssh远程
是能帮我解释这段代码 ?
public void uploadSingleDataToServer(String fromLocalFile,
String toServerFile) throws Exception {
//本地执行判断
if (new File(fromLocalFile).isDirectory())
{
throw new UploadFileNotSuccessException(fromLocalFile);
}
FileInputStream fis = null;
// exec 'scp -t toServerFile' remotely
//logger.putMsg(Logger.INFO,"Ready to transfer local file '" + fromLocalFile+ "' to server directory '" + toServerFile + "'");
String command = "scp -p -t " + toServerFile;
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
// get I/O streams for remote scp
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
channel.connect();
//logger.putMsg(Logger.INFO,"Connection channel established succesfully");
//logger.putMsg(Logger.INFO,"Start to upload");
if (SSHExecUtil.checkAck(in) != 0) {
System.exit(0);
}
// send "C0644 filesize filename", where filename should not include '/'
long filesize = (new File(fromLocalFile)).length();
command = "C0644 " + filesize + " ";
if (fromLocalFile.lastIndexOf('/') > 0) {
command += fromLocalFile
.substring(fromLocalFile.lastIndexOf('/') + 1);
} else if (fromLocalFile.lastIndexOf('\\') > 0) {
command += fromLocalFile
.substring(fromLocalFile.lastIndexOf('\\') + 1);
}
else {
command += fromLocalFile;
}
command += "\n";
out.write(command.getBytes());
out.flush();
if (SSHExecUtil.checkAck(in) != 0) {
//logger.putMsg(Logger.INFO,fromLocalFile + "check fails");
return;
}
// send a content of lfile
fis = new FileInputStream(fromLocalFile);
byte[] buf = new byte[1024];
while (true) {
int len = fis.read(buf, 0, buf.length);
if (len <= 0)
break;
out.write(buf, 0, len); // out.flush();
}
fis.close();
fis = null;
// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();
if (SSHExecUtil.checkAck(in) != 0) {
//logger.putMsg(Logger.ERROR,toServerFile + "check fails");
return;
}
out.close();
//logger.putMsg(Logger.INFO,"Upload success");
channel.disconnect();
//logger.putMsg(Logger.INFO,"channel disconnect");
}