Java Code Examples for com.jcraft.jsch.ChannelSftp#exit()

The following examples show how to use com.jcraft.jsch.ChannelSftp#exit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SFtpClientUtils.java    From bamboobsc with Apache License 2.0 7 votes vote down vote up
public static void rm(String user, String password, String addr, int port, List<String> fileName) throws JSchException, SftpException, Exception {
	
	if (fileName==null || fileName.size()<1) {
		return;
	}
	Session session = getSession(user, password, addr, port);				
	Channel channel = session.openChannel("sftp");
	channel.connect();
	ChannelSftp sftpChannel = (ChannelSftp) channel;		
	try {
		for (String f : fileName) {
			sftpChannel.rm(f);				
			logger.warn("success remove file from " + addr + " :" + fileName);				
		}
	} catch (Exception e) {
		e.printStackTrace();
		throw e;
	} finally {
		sftpChannel.exit();
		channel.disconnect();
		session.disconnect();				
	}		
}
 
Example 2
Source File: SFtpClientUtils.java    From bamboobsc with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static Vector<LsEntry> getRemoteFileList(String user, String password, String addr, int port, String cwd) throws JSchException, 
	SftpException, Exception {
	
	Session session = getSession(user, password, addr, port);	
	Vector<LsEntry> lsVec=null;
	Channel channel = session.openChannel("sftp");
	channel.connect();
	ChannelSftp sftpChannel = (ChannelSftp) channel;
	try {
		lsVec=(Vector<LsEntry>)sftpChannel.ls(cwd); //sftpChannel.lpwd()
	} catch (Exception e) {
		e.printStackTrace();
		throw e;
	} finally {
		sftpChannel.exit();
		channel.disconnect();
		session.disconnect();			
	}		
	return lsVec;		
}
 
Example 3
Source File: SFtpClientUtils.java    From bamboobsc with Apache License 2.0 6 votes vote down vote up
/**
 * 本地檔案放到遠端SFTP上
 * 	
 * @param user
 * @param password
 * @param addr
 * @param port
 * @param localFile
 * @param remoteFile
 * @throws JSchException
 * @throws SftpException
 * @throws Exception
 */
public static void put(String user, String password, String addr, int port,
		List<String> localFile, List<String> remoteFile) throws JSchException, SftpException, Exception {
	
	Session session = getSession(user, password, addr, port);	
	Channel channel = session.openChannel("sftp");
	channel.connect();
	ChannelSftp sftpChannel = (ChannelSftp) channel;		
	try {
		for (int i=0; i<localFile.size(); i++) {
			String rf=remoteFile.get(i);
			String lf=localFile.get(i);
			logger.info("put local file: " + lf + " write to " + addr + " :" + rf );
			sftpChannel.put(lf, rf);
			logger.info("success write to " + addr + " :" + rf);
		}
	} catch (Exception e) {
		e.printStackTrace();
		throw e;
	} finally {
		sftpChannel.exit();
		channel.disconnect();
		session.disconnect();				
	}		
}
 
Example 4
Source File: SFtpClientUtils.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
/**
 * 抓遠端檔案然後存到本機 , 單筆
 * 
 * @param user
 * @param password
 * @param addr
 * @param port
 * @param remoteFile
 * @param localFile
 * @throws JSchException
 * @throws SftpException
 * @throws Exception
 */
public static void get(String user, String password, String addr, int port, 
		String remoteFile, String localFile) throws JSchException, SftpException, Exception {
			
	Session session = getSession(user, password, addr, port);
	Channel channel = session.openChannel("sftp");
	channel.connect();
	ChannelSftp sftpChannel = (ChannelSftp) channel;		
	logger.info("get remote file: " + remoteFile + " write to:" + localFile );	
	try {
		sftpChannel.get(remoteFile, localFile);
	} catch (Exception e) {
		e.printStackTrace();
		throw e;
	} finally {
		sftpChannel.exit();
		channel.disconnect();
		session.disconnect();			
	}
	File f=new File(localFile);
	if (!f.exists()) {
		f=null;
		logger.error("get remote file:"+remoteFile + " fail!");
		throw new Exception("get remote file:"+remoteFile + " fail!");
	}
	f=null;
	logger.info("success write:" + localFile);
}
 
Example 5
Source File: SFtpClientUtils.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
/**
 * 抓遠端檔案然後存到本機 , 多筆
 * 
 * @param user
 * @param password
 * @param addr
 * @param port
 * @param remoteFile
 * @param localFile
 * @throws JSchException
 * @throws SftpException
 * @throws Exception
 */
public static void get(String user, String password, String addr, int port, 
		List<String> remoteFile, List<String> localFile) throws JSchException, SftpException, Exception {
			
	Session session = getSession(user, password, addr, port);	
	Channel channel = session.openChannel("sftp");
	channel.connect();
	ChannelSftp sftpChannel = (ChannelSftp) channel;		
	try {
		for (int i=0; i<remoteFile.size(); i++) {
			String rf=remoteFile.get(i);
			String lf=localFile.get(i);
			logger.info("get remote file: " + rf + " write to:" + lf );
			sftpChannel.get(rf, lf);
			File f=new File(lf);
			if (!f.exists()) {
				f=null;
				logger.error("get remote file:"+rf + " fail!");
				throw new Exception("get remote file:"+rf + " fail!");
			}
			f=null;
			logger.info("success write:" + lf);
		}
	} catch (Exception e) {
		e.printStackTrace();
		throw e;
	} finally {
		sftpChannel.exit();
		channel.disconnect();
		session.disconnect();				
	}
}
 
Example 6
Source File: SftpFileTransferLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenUploadFileUsingJsch_thenSuccess() throws JSchException, SftpException {
    ChannelSftp channelSftp = setupJsch();
    channelSftp.connect();
    channelSftp.put(localFile, remoteDir + "jschFile.txt");
    channelSftp.exit();
}
 
Example 7
Source File: SftpFileTransferLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenDownloadFileUsingJsch_thenSuccess() throws JSchException, SftpException {
    ChannelSftp channelSftp = setupJsch();
    channelSftp.connect();
    channelSftp.get(remoteFile, localDir + "jschFile.txt");
    channelSftp.exit();
}