本文整理汇总了Java中com.jcraft.jsch.ChannelSftp.getHome方法的典型用法代码示例。如果您正苦于以下问题:Java ChannelSftp.getHome方法的具体用法?Java ChannelSftp.getHome怎么用?Java ChannelSftp.getHome使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jcraft.jsch.ChannelSftp
的用法示例。
在下文中一共展示了ChannelSftp.getHome方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
示例2: 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();
}