本文整理匯總了Java中com.jcraft.jsch.ChannelSftp.connect方法的典型用法代碼示例。如果您正苦於以下問題:Java ChannelSftp.connect方法的具體用法?Java ChannelSftp.connect怎麽用?Java ChannelSftp.connect使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.jcraft.jsch.ChannelSftp
的用法示例。
在下文中一共展示了ChannelSftp.connect方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: 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();
}
示例3: 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);
}
}
示例4: 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();
}
示例5: init
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
public SftpClient init() {
try {
String host = PropertiesUtil.getString("sftp.host");
int port = PropertiesUtil.getInt("sftp.port");
String userName = PropertiesUtil.getString("sftp.user.name");
String password = PropertiesUtil.getString("sftp.user.password");
Integer timeout = PropertiesUtil.getInt("sftp.timeout");
Integer aliveMax = PropertiesUtil.getInt("sftp.aliveMax");
JSch jsch = new JSch(); // 創建JSch對象
session = jsch.getSession(userName, host, port); // 根據用戶名,主機ip,端口獲取一個Session對象
if (password != null) {
session.setPassword(password); // 設置密碼
}
session.setConfig("StrictHostKeyChecking", "no"); // 為Session對象設置properties
if (timeout != null) session.setTimeout(timeout); // 設置timeout時間
if (aliveMax != null) session.setServerAliveCountMax(aliveMax);
session.connect(); // 通過Session建立鏈接
channel = (ChannelSftp)session.openChannel("sftp"); // 打開SFTP通道
channel.connect(); // 建立SFTP通道的連接
logger.info("SSH Channel connected.");
} catch (JSchException e) {
throw new FtpException("", e);
}
return this;
}
示例6: 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);
}
示例7: ls
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
public static List<String> ls(String hostname, int port, String username, File keyFile, final String passphrase,
String path) throws JSchException, IOException, SftpException {
Session session = createSession(hostname, port, username, keyFile, passphrase);
session.connect();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
@SuppressWarnings("unchecked")
Vector<LsEntry> vector = (Vector<LsEntry>) channel.ls(path);
channel.disconnect();
session.disconnect();
List<String> files = new ArrayList<String>();
for (LsEntry lse : vector) {
files.add(lse.getFilename());
}
return files;
}
示例8: upload
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
/**
* Perform the specified SSH file upload.
*
* @param from source local file URI ({@code file} protocol)
* @param to target remote folder URI ({@code ssh} protocol)
*/
private static void upload(URI from, URI to) {
try (SessionHolder<ChannelSftp> session = new SessionHolder<>(ChannelType.SFTP, to);
FileInputStream fis = new FileInputStream(new File(from))) {
LOG.info("Uploading {} --> {}", from, session.getMaskedUri());
ChannelSftp channel = session.getChannel();
channel.connect();
channel.cd(to.getPath());
channel.put(fis, getName(from.getPath()));
} catch (Exception e) {
throw new RemoteFileUploadFailedException("Cannot upload file", e);
}
}
示例9: connect
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
public void connect() {
if (session != null) {
throw new IllegalStateException("Already Connected");
}
try {
JSch jSch = new JSch();
if (knownHosts != null) {
jSch.setKnownHosts(knownHosts);
}
if (identity != null) {
jSch.addIdentity(identity);
}
session = jSch.getSession(user, host, 22);
Properties properties = new Properties();
properties.setProperty("StrictHostKeyChecking", "no");
properties.setProperty("PreferredAuthentications", "publickey");
session.setConfig(properties);
session.connect();
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
LOGGER.info("connect() - Connected to %s", host);
} catch (Exception ex) {
// Failed to connect
session = null;
LOGGER.error("connect() - Failed to connect to %s - %s ", host, ex.getMessage());
throw new RuntimeException(ex);
}
}
示例10: uploadFile
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
public static void uploadFile(String hostname, int port, String username, File keyFile, final String passphrase,
File file, String destinationFolder) throws JSchException, IOException, SftpException {
Session session = createSession(hostname, port, username, keyFile, passphrase);
session.connect();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
channel.cd(destinationFolder);
channel.put(new FileInputStream(file), file.getName());
channel.disconnect();
session.disconnect();
}
示例11: connectAndGetSftpChannel
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
public ChannelSftp connectAndGetSftpChannel() throws JSchException {
JSch jsch = new JSch();
jsch.setKnownHosts(context.getFilesDir().getAbsolutePath() + File.separator + "jsch_known_hosts");
String username = prefs.sshUserName().getOr(null);
String host = prefs.sshServerAddress().getOr(null);
String port = prefs.sshServerPort().getOr(null);
if (username == null || host == null || port == null)
throw new IllegalStateException("Please enter your server settings first");
Session session = jsch.getSession(username, host, Integer.parseInt(port));
UserInfo info = storedOrAskedUserInfo;
session.setUserInfo(info);
try {
session.connect(5000);
storedOrAskedUserInfo.confirmPasswordRight();
}
catch (JSchException je) {
if ("auth fail".equals(je.getMessage().toLowerCase())) { // undocumented!
storedOrAskedUserInfo.clearPassword();
}
throw je;
}
ChannelSftp channel = (ChannelSftp)session.openChannel("sftp");
channel.connect(5000);
return channel;
}
示例12: _testConnectionSftp
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
private void _testConnectionSftp(String user, String password, String host, int port, IMoverInterface callbackContext) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
OutputStream output = channel.put(mTestFilename);
output.write("Hello Alto".getBytes());
output.close();
channel.rm(mTestFilename);
channel.disconnect();
session.disconnect();
callbackContext.success();
} catch (Exception e) {
callbackContext.error(e.getMessage());
}
}
示例13: _connectSftp
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
private void _connectSftp(String user, String password, String host, int port, IMoverInterface callbackContext) throws JSchException {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
String key = UUID.randomUUID().toString();
mSftpChannels.put(key, channel);
callbackContext.success(key);
}
示例14: put
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
public void put(Path localPath, String remotePath) throws JSchException, SftpException {
connectIfNot();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
try {
channel.connect();
logger.info("sftp put, from={}, to={}", localPath, remotePath);
channel.put(new ByteArrayInputStream(Files.bytes(localPath)), remotePath);
} finally {
channel.disconnect();
}
}
示例15: openConnectedChannel
import com.jcraft.jsch.ChannelSftp; //導入方法依賴的package包/類
/**
* Open and Connect a new SFTP channel.
*
* @return a new channel.
*/
protected ChannelSftp openConnectedChannel() throws JSchException {
Session session = openSession();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
try {
channel.cd(basePath);
} catch (SftpException e) {
throw new IoException(e);
}
return channel;
}