日期:2014-05-16  浏览次数:20375 次

jsch 深入浅出(二)

?

在上面一个帖子里就简单介绍了如何基于jsch实现ssh.

下面就简单介绍一下如何实现FTP的功能通过JSCH.

public class JftpHandler extends JschHandler {
	private static final Logger log = LoggerFactory
			.getLogger(JftpHandler.class);

	private ChannelSftp sftp = null;

	public JftpHandler(String username, String host, int port, String identity) {
		super(username, host, port, identity);
	}

	public JftpHandler(String username, String host, int port, UserInfo userInfo) {
		super(username, host, port, userInfo);
	}

	@Override
	public void init() throws JSchException {
		super.init();
		sftp = (ChannelSftp) session.openChannel(SFTP_PROTOCAL);//sftp
		sftp.connect();
		log.info("Jftp connection success.");
	}

	@Override
	public void destory() {
		if (sftp != null) {
			sftp.quit();
			sftp.disconnect();
		}
		super.destory();

		log.info("Jftp destory success.");
	}

	public void cd(String path) throws SftpException {
		sftp.cd(path);
	}

	public void lcd(String path) throws SftpException {
		sftp.lcd(path);
	}

	public String lpwd() {
		return sftp.lpwd();
	}

	public String pwd() throws SftpException {
		return sftp.pwd();
	}

	public void mkdir(String path) throws SftpException {
		sftp.mkdir(path);
	}

	public void rm(String path) throws SftpException {
		sftp.rm(path);
	}

	public void quit() {
		sftp.quit();
	}

	public void rmdir(String path) throws SftpException {
		sftp.rmdir(path);
	}

	public void exit() {
		sftp.exit();
	}

	public void put(String src, String dst, int mode) throws SftpException {
		sftp.put(src, dst, mode);
	}

	public void put(String src, String dst) throws SftpException {
		put(src, dst, 0);
	}

	public void put(String dst) throws SftpException {
		put(dst, ".");
	}

	public void get(String src, String dst) throws SftpException {
		sftp.get(src, dst);
	}

	public void get(String src) throws SftpException {
		get(src, ".");
	}

	/**
	 * Changes the permissions of one or several remote files.
	 * 
	 * @param permissions
	 * @param path
	 * @throws SftpException
	 */
	public void chmod(int permissions, String path) throws SftpException {
		sftp.chmod(permissions, path);
	}

	/**
	 * @returns the protocol version number supported by this client
	 */
	public String version() {
		return sftp.version();
	}

}
??上面的代码只是对JSCH的简单包装,也是对JSCH的抛砖引玉。希望对大家有点用处。。。

?