本文整理汇总了Java中com.jcraft.jsch.ChannelSftp.exit方法的典型用法代码示例。如果您正苦于以下问题:Java ChannelSftp.exit方法的具体用法?Java ChannelSftp.exit怎么用?Java ChannelSftp.exit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jcraft.jsch.ChannelSftp
的用法示例。
在下文中一共展示了ChannelSftp.exit方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: fileFetch
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public static void fileFetch(String host, String user, String keyLocation, String sourceDir, String destDir) {
JSch jsch = new JSch();
Session session = null;
try {
// set up session
session = jsch.getSession(user,host);
// use private key instead of username/password
session.setConfig(
"PreferredAuthentications",
"publickey,gssapi-with-mic,keyboard-interactive,password");
jsch.addIdentity(keyLocation);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
// copy remote log file to localhost.
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.get(sourceDir, destDir);
channelSftp.exit();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.disconnect();
}
}
示例2: getRemoteFileList
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@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;
}
示例3: listFiles
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
* Lists directory files on remote server.
* @throws URISyntaxException
* @throws JSchException
* @throws SftpException
*/
private void listFiles() throws URISyntaxException, JSchException, SftpException {
JSch jsch = new JSch();
JSch.setLogger(new JschLogger());
setupSftpIdentity(jsch);
URI uri = new URI(sftpUrl);
Session session = jsch.getSession(sshLogin, uri.getHost(), 22);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
System.out.println("Connected to SFTP server");
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
Vector<LsEntry> directoryEntries = sftpChannel.ls(uri.getPath());
for (LsEntry file : directoryEntries) {
System.out.println(String.format("File - %s", file.getFilename()));
}
sftpChannel.exit();
session.disconnect();
}
示例4: startSession
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public boolean startSession(String file, String host) throws JSchException, SftpException {
String path = BioNimbusConfig.get().getDataFolder();
try {
session = jsch.getSession(USER, host, PORT);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(PASSW);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
LOGGER.info("Downloading file");
sftpChannel.get(path + file, path);
sftpChannel.exit();
session.disconnect();
} catch (JSchException | SftpException e) {
return false;
}
return true;
}
示例5: put
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
* 本地檔案放到遠端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();
}
}
示例6: transferFile
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
* Transfer a file to remote destination via JSCH library using sFTP protocol
*
* @param username String remote SFTP server user name.
* @param password String remote SFTP server user password
* @param host String remote SFTP server IP address or host name.
* @param file File to transfer to SFTP Server.
* @param transferProtocol protocol to transfer a file. {@link FileTransferProtocol}
* @return boolean true if file is transfered otherwise false.
* @throws ApplicationException
*/
public static boolean transferFile(final String username, final String password, final String host,
final File file, final FileTransferProtocol transferProtocol)
throws ApplicationException {
// currently can deal with sftp only.
LOGGER.trace("Invoking transferFile...");
JSch jsch = new JSch();
try {
Session session = jsch.getSession(username, host);
LOGGER.debug("Session Host: " + session.getHost());
session.setPassword(password);
Properties properties = new Properties();
properties.put("StrictHostKeyChecking", "no");
session.setConfig(properties);
LOGGER.debug("Connecting to a session Host Server...");
session.connect();
LOGGER.debug("session is established with host server.");
Channel channel = session.openChannel(transferProtocol.ftpStringRepresentation());
LOGGER.debug("Connecting to a sftp Channel...");
channel.connect();
LOGGER.debug("Connected with sftp Channel.");
ChannelSftp channelSftp = (ChannelSftp) channel;
channelSftp.put(new FileInputStream(file), file.getName());
LOGGER.debug("File transfered successfully");
channelSftp.exit();
LOGGER.debug("sftp channel disconnected.");
channel.disconnect();
LOGGER.debug("channel disconnected.");
session.disconnect();
LOGGER.debug("session disconnected.");
return true;
} catch (JSchException | FileNotFoundException | SftpException e) {
LOGGER.error(e.getMessage(), e.getCause());
throw new ApplicationException(e.getMessage(), ApplicationSeverity.ERROR, e.getCause(), e);
}
}
示例7: upload
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public void upload(File srcFile, String destFile, long timeout) throws JSchException, SftpException, IOException {
Channel channel = this.session.openChannel("sftp");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect();
channels.add(channel);
new ChanneOperationTimer(channel, timeout).start();
ChannelSftp sftp = (ChannelSftp) channel;
sftp.put(FileUtils.openInputStream(srcFile), destFile);
sftp.exit();
}
示例8: download
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public void download(String srcFile, File destFile, long timeout) throws JSchException, SftpException, IOException {
Channel channel = this.session.openChannel("sftp");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect();
channels.add(channel);
new ChanneOperationTimer(channel, timeout).start();
ChannelSftp sftp = (ChannelSftp) channel;
try (FileOutputStream out = FileUtils.openOutputStream(destFile)) {
sftp.get(srcFile, out);
} finally {
sftp.exit();
}
}
示例9: get
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
* 抓遠端檔案然後存到本機 , 單筆
*
* @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);
}
示例10: rm
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
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();
}
}
示例11: doSFTPUpload
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@Override
public void doSFTPUpload(String sftpHostname, String sftpUsername, String sftpPassword, String file, String fileName) {
LOG.trace("************************************doSFTPUpload() started************************************");
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(sftpUsername, sftpHostname, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(sftpPassword);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
sftpChannel.mkdir("");
sftpChannel.put(new FileInputStream(file), fileName);
sftpChannel.exit();
session.disconnect();
} catch (Exception e) {
LOG.error("Exception performing SFTP upload of " + file + " to " + sftpHostname, e);
throw new RuntimeException(e);
}
LOG.trace("************************************doSFTPUpload() completed************************************");
}
示例12: upload
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public void upload(Map<String,String> files) throws Exception {
JSch jsch = new JSch();
Session session = null;
session = jsch.getSession(user, host, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftp = (ChannelSftp)channel;
for(Map.Entry<String, String> ff : files.entrySet()) {
// key - source, value - destination
sftp.put(ff.getKey(), ff.getValue());
}
sftp.exit();
session.disconnect();
}
示例13: put
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public void put(String content, String fileName) throws Exception {
ChannelSftp channel = null;
try {
channel = openChannel();
channel.put(new ByteArrayInputStream(content.getBytes()), fileName);
} finally {
if (channel != null) {
channel.exit();
}
}
}