本文整理匯總了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();
}