?
在上面一個(gè)帖子里就簡(jiǎn)單介紹了如何基于jsch實(shí)現(xiàn)ssh.
下面就簡(jiǎn)單介紹一下如何實(shí)現(xiàn)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();
}
}
??上面的代碼只是對(duì)JSCH的簡(jiǎn)單包裝,也是對(duì)JSCH的拋磚引玉。希望對(duì)大家有點(diǎn)用處。。。
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

