本文整理汇总了Java中com.jcraft.jsch.ChannelSftp.cd方法的典型用法代码示例。如果您正苦于以下问题:Java ChannelSftp.cd方法的具体用法?Java ChannelSftp.cd怎么用?Java ChannelSftp.cd使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jcraft.jsch.ChannelSftp
的用法示例。
在下文中一共展示了ChannelSftp.cd方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: upload
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private void upload(String filename) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession(login, server, port);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp channelSftp = (ChannelSftp) channel;
channelSftp.cd(workingDirectory);
File f = new File(filename);
channelSftp.put(new FileInputStream(f), f.getName());
f.delete();
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例2: getDir
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private void getDir(final ChannelSftp channel,
final String remoteFile,
final File localFile) throws IOException, SftpException {
String pwd = remoteFile;
if (remoteFile.lastIndexOf('/') != -1) {
if (remoteFile.length() > 1) {
pwd = remoteFile.substring(0, remoteFile.lastIndexOf('/'));
}
}
channel.cd(pwd);
if (!localFile.exists()) {
localFile.mkdirs();
}
@SuppressWarnings("unchecked")
final List<ChannelSftp.LsEntry> files = channel.ls(remoteFile);
for (ChannelSftp.LsEntry le : files) {
final String name = le.getFilename();
if (le.getAttrs().isDir()) {
if (".".equals(name) || "..".equals(name)) {
continue;
}
getDir(channel,
channel.pwd() + "/" + name + "/",
new File(localFile, le.getFilename()));
} else {
getFile(channel, le, localFile);
}
}
channel.cd("..");
}
示例3: upload
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
* Creates a new file on the remote host using the input content.
*
* @param from the byte array content to be uploaded
* @param fileName the name of the file for which the content will be saved into
* @param toPath the path of the file for which the content will be saved into
* @param isUserHomeBased true if the path of the file is relative to the user's home directory
* @param filePerm file permissions to be set
* @throws Exception exception thrown
*/
public void upload(InputStream from, String fileName, String toPath, boolean isUserHomeBased, String filePerm) throws Exception {
ChannelSftp channel = (ChannelSftp) this.session.openChannel("sftp");
channel.connect();
String absolutePath = isUserHomeBased ? channel.getHome() + "/" + toPath : toPath;
String path = "";
for (String dir : absolutePath.split("/")) {
path = path + "/" + dir;
try {
channel.mkdir(path);
} catch (Exception ee) {
}
}
channel.cd(absolutePath);
channel.put(from, fileName);
if (filePerm != null) {
channel.chmod(Integer.parseInt(filePerm), absolutePath + "/" + fileName);
}
channel.disconnect();
}
示例4: renameFile
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@Override
public boolean renameFile(String fromFilePath, String toFilePath, boolean closeSession) {
ChannelSftp sftp = null;
try {
// Get a reusable channel if the session is not auto closed.
sftp = (closeSession) ? openConnectedChannel() : openConnectedChannel(CHANNEL_1);
sftp.cd(basePath);
sftp.rename(fromFilePath, toFilePath);
return true;
} catch (Exception e) {
return false;
} finally {
if (closeSession) {
close();
}
}
}
示例5: download
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
* Perform the specified SSH file download.
*
* @param from source remote file URI ({@code ssh} protocol)
* @param to target local folder URI ({@code file} protocol)
*/
private static void download(URI from, URI to) {
File out = new File(new File(to), getName(from.getPath()));
try (SessionHolder<ChannelSftp> session = new SessionHolder<>(ChannelType.SFTP, from);
OutputStream os = new FileOutputStream(out);
BufferedOutputStream bos = new BufferedOutputStream(os)) {
LOG.info("Downloading {} --> {}", session.getMaskedUri(), to);
ChannelSftp channel = session.getChannel();
channel.connect();
channel.cd(getFullPath(from.getPath()));
channel.get(getName(from.getPath()), bos);
} catch (Exception e) {
throw new RemoteFileDownloadFailedException("Cannot download file", e);
}
}
示例6: copyFile
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@Override
public void copyFile(String fromFilePath, String toFilePath, boolean closeSession) {
ChannelSftp uploadSftp = null;
InputStream inputStream = null;
try {
// Get a reusable channel if the session is not auto closed.
uploadSftp = (closeSession) ? openConnectedChannel() : openConnectedChannel(CHANNEL_1);
uploadSftp.cd(basePath);
inputStream = getInputStream(fromFilePath, true);
uploadSftp.put(inputStream, toFilePath);
} catch (Exception e) {
throw new IoException("Error copying file. Error %s", e.getMessage());
} finally {
if (closeSession) {
close();
}
}
}
示例7: copyToDir
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@Override
public void copyToDir(String fromFilePath, String toDirPath, boolean closeSession) {
ChannelSftp uploadSftp = null;
FileInfo fileInfo = new FileInfo(fromFilePath, false, new java.util.Date().getTime(), -1);
InputStream inputStream = null;
try {
// Get a reusable channel if the session is not auto closed.
uploadSftp = (closeSession) ? openConnectedChannel() : openConnectedChannel(CHANNEL_1);
uploadSftp.cd(basePath);
if (!toDirPath.endsWith("/")) {
toDirPath += "/";
}
inputStream = getInputStream(fromFilePath, true);
uploadSftp.put(inputStream, toDirPath + fileInfo.getName());
} catch (Exception e) {
throw new IoException("Error copying directory. Error %s", e.getMessage());
} finally {
if (closeSession) {
close();
}
}
}
示例8: moveToDir
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@Override
public void moveToDir(String fromFilePath, String toDirPath, boolean closeSession) {
ChannelSftp sftp = null;
FileInfo fileInfo = new FileInfo(fromFilePath, false, new java.util.Date().getTime(), -1);
try {
// Get a reusable channel if the session is not auto closed.
sftp = (closeSession) ? openConnectedChannel() : openConnectedChannel(CHANNEL_1);
sftp.cd(basePath);
if (!toDirPath.endsWith("/")) {
toDirPath += "/";
}
sftp.rename(fromFilePath, toDirPath + fileInfo.getName());
} catch (Exception e) {
throw new IoException("Error moving (renaming) directory. Error %s", e.getMessage());
} finally {
if (closeSession) {
close();
}
}
}
示例9: getInputStream
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@Override
public InputStream getInputStream(String relativePath, boolean mustExist, boolean closeSession) {
Session session = null;
ChannelSftp sftp = null;
try {
session = openSession();
// Get a reusable channel if the session is not auto closed.
sftp = (closeSession) ? openConnectedChannel() : openConnectedChannel(CHANNEL_IN);
sftp.cd(basePath);
if (mustExist && !fileExists(sftp, relativePath)) {
throw new IoException("Could not find endpoint '%s' that was configured as MUST EXIST",relativePath);
}
return new CloseableInputStream(sftp.get(relativePath), session, sftp, closeSession);
} catch (Exception e) {
if (e instanceof IoException ||
(e instanceof SftpException && ((SftpException) e).id != 2)) {
throw new IoException("Error getting the input stream for sftp endpoint. Error %s", e.getMessage());
} else {
return null;
}
}
}
示例10: download
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
* Downloads the content of a file from the remote host as a String.
*
* @param fileName the name of the file for which the content will be downloaded
* @param fromPath the path of the file for which the content will be downloaded
* @param isUserHomeBased true if the path of the file is relative to the user's home directory
* @return the content of the file
* @throws Exception exception thrown
*/
public String download(String fileName, String fromPath, boolean isUserHomeBased) throws Exception {
ChannelSftp channel = (ChannelSftp) this.session.openChannel("sftp");
channel.connect();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BufferedOutputStream buff = new BufferedOutputStream(outputStream);
String absolutePath = isUserHomeBased ? channel.getHome() + "/" + fromPath : fromPath;
channel.cd(absolutePath);
channel.get(fileName, buff);
channel.disconnect();
return outputStream.toString();
}
示例11: openSftpChannel
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
* Opens a new SFTP channel over this SSH session.
*
* @throws JSchException
* @throws SftpException
* @see JSchSftpChannel
*/
public JSchSftpChannel openSftpChannel() throws JSchException, SftpException {
ChannelSftp chan = (ChannelSftp) session.openChannel("sftp");
chan.connect(timeout);
if (url.getPath().isPresent()) {
String dir = url.getPath().get();
try {
chan.cd(dir);
} catch (SftpException e) {
logger.warning(e.toString());
mkdirs(chan, dir);
chan.cd(dir);
}
}
return new JSchSftpChannel(chan);
}
示例12: moveFile
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@Override
public void moveFile(String fromFilePath, String toFilePath, boolean closeSession) {
ChannelSftp sftp = null;
try {
// Get a reusable channel if the session is not auto closed.
sftp = (closeSession) ? openConnectedChannel() : openConnectedChannel(CHANNEL_1);
sftp.cd(basePath);
sftp.rename(fromFilePath, toFilePath);
} catch (Exception e) {
throw new IoException("Error moving (renaming) file. Error %s", e.getMessage());
} finally {
if (closeSession) {
close();
}
}
}
示例13: sendDirectoryToRemote
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private void sendDirectoryToRemote(final ChannelSftp channel,
final Directory directory)
throws IOException, SftpException {
final String dir = directory.getDirectory().getName();
try {
channel.stat(dir);
} catch (final SftpException e) {
// dir does not exist.
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
channel.mkdir(dir);
channel.chmod(getDirMode(), dir);
}
}
channel.cd(dir);
sendDirectory(channel, directory);
channel.cd("..");
}
示例14: copyFile
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
*
* @param sourceFilePath
* @param destinationFilePath
* @param destinationHost
* @param destinationPort
* @param destinationUserName
* @param destinationUserPassword
* @param destinationPrivateKeyFilePath
* @param destinationPrivateKeyPassphrase
* @throws IOException
* @throws JSchException
* @throws SftpException
*/
public static void copyFile(
String sourceFilePath,
String destinationFilePath,
String destinationHost,
String destinationPort,
String destinationUserName,
String destinationUserPassword,
String destinationPrivateKeyFilePath,
String destinationPrivateKeyPassphrase) throws IOException, JSchException, SftpException {
JSch jsch = new JSch();
if (!StringUtils.isEmptyOrNull(destinationPrivateKeyFilePath)) {
jsch.addIdentity(destinationPrivateKeyFilePath, destinationPrivateKeyPassphrase);
}
int destinationPortNumber = 22;
if (!StringUtils.isEmptyOrNull(destinationPort)) {
destinationPortNumber = Integer.parseInt(destinationPort);
}
Session session = jsch.getSession(destinationUserName, destinationHost, destinationPortNumber);
if (!StringUtils.isEmptyOrNull(destinationUserPassword)) {
session.setPassword(destinationUserPassword);
}
// Properties config = new Properties();
// config.put("StrictHostKeyChecking", "no");
// session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp channelSftp = (ChannelSftp)channel;
File destinationFile = new File(destinationFilePath);
channelSftp.cd(destinationFile.getPath());
channelSftp.put(new FileInputStream(sourceFilePath), destinationFile.getName());
}
示例15: SshToolInvocation
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public SshToolInvocation(ToolDescription desc, SshNode workerNodeA,
AskUserForPw askUserForPwA, CredentialManager credentialManager)
throws JSchException, SftpException {
this.workerNode = workerNodeA;
this.credentialManager = credentialManager;
setRetrieveData(workerNodeA.isRetrieveData());
this.askUserForPw = askUserForPwA;
usecase = desc;
ChannelSftp sftp = SshPool.getSftpPutChannel(workerNode, askUserForPw);
synchronized (getNodeLock(workerNode)) {
logger.info("Changing remote directory to "
+ workerNode.getDirectory());
sftp.cd(workerNode.getDirectory());
Random rnd = new Random();
while (true) {
tmpname = "usecase" + rnd.nextLong();
try {
sftp.lstat(workerNode.getDirectory() + tmpname);
continue;
} catch (Exception e) {
// file seems to not exist :)
}
sftp.mkdir(workerNode.getDirectory() + tmpname);
sftp.cd(workerNode.getDirectory() + tmpname);
break;
}
}
}