本文整理汇总了Java中com.jcraft.jsch.ChannelSftp.mkdir方法的典型用法代码示例。如果您正苦于以下问题:Java ChannelSftp.mkdir方法的具体用法?Java ChannelSftp.mkdir怎么用?Java ChannelSftp.mkdir使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jcraft.jsch.ChannelSftp
的用法示例。
在下文中一共展示了ChannelSftp.mkdir方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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("..");
}
示例3: 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;
}
}
}
示例4: upload
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public static void upload(String directory, String uploadFile, ChannelSftp sftp) throws Exception{
File file = new File(uploadFile);
if(file.exists()){
try {
Vector content = sftp.ls(directory);
if(content == null){
sftp.mkdir(directory);
System.out.println("mkdir:" + directory);
}
} catch (SftpException e) {
sftp.mkdir(directory);
}
sftp.cd(directory);
System.out.println("directory: " + directory);
if(file.isFile()){
InputStream ins = new FileInputStream(file);
sftp.put(ins, new String(file.getName().getBytes(),"UTF-8"));
}else{
File[] files = file.listFiles();
for (File file2 : files) {
String dir = file2.getAbsolutePath();
if(file2.isDirectory()){
String str = dir.substring(dir.lastIndexOf(file2.separator));
directory = directory + str;
}
System.out.println("directory is :" + directory);
upload(directory,dir,sftp);
}
}
}
}
示例5: makeSubDir
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private void makeSubDir(ChannelSftp sftp, String directory, int index, HashSet<String> existAlready) throws SftpException {
int delim = directory.indexOf('/', index);
final String sub = (delim == -1) ? directory : directory.substring(0, delim);
if (existAlready == null || !existAlready.contains(sub)) {
System.out.println("Making directory " + sub);
try {
sftp.mkdir(sub);
if (existAlready != null)
existAlready.add(sub);
} catch (SftpException e) {
// still may exist remotely
if (e.id != ChannelSftp.SSH_FX_FAILURE) {
System.err.println(sub + " may exist already: got err#" + e.id);
throw e;
}
if (existAlready != null)
existAlready.add(sub);
}
}
if (delim != -1 && delim != directory.length()-1) {
makeSubDir(sftp, directory, delim + 1, existAlready);
}
}
示例6: createRemotePath
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private void createRemotePath(@NotNull final ChannelSftp channel,
@NotNull final String destination) throws SftpException {
final int endIndex = destination.lastIndexOf('/');
if (endIndex > 0) {
createRemotePath(channel, destination.substring(0, endIndex));
}
try {
channel.stat(destination);
} catch (SftpException e) {
// dir does not exist.
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
channel.mkdir(destination);
}
}
}
示例7: createRelativePathDirectoriesIfNecessary
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private void createRelativePathDirectoriesIfNecessary(ChannelSftp sftp, String relativePath, boolean mustExist) {
String[] elements = StringUtils.split(relativePath, "/");
if (elements.length == 1) {
// if there is only one element, it's the filename itself. No
// directories to create
return;
} else {
try {
for (int i = 0; i < elements.length - 1; i++) {
try {
sftp.cd(elements[i]);
} catch (SftpException cdex) {
// if we can't change to the directory, try and create
// it
sftp.mkdir(elements[i]);
sftp.cd(elements[i]);
}
}
for (int i = 0; i < elements.length - 1; i++) {
sftp.cd("../");
}
} catch (SftpException mkdirex) {
log.error("Error writing to Sftp site. Unable to create relative directory %s. " + "Error %s", relativePath,
mkdirex.getMessage());
throw new IoException(mkdirex);
}
}
}
示例8: 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************************************");
}
示例9: createFolder
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@Override
public String createFolder(String parentPath, String newDirName)
throws Exception {
try {
ChannelSftp c = init(parentPath);
String newPath = concatPaths(parentPath, newDirName);
c.mkdir(extractSessionPath(newPath));
tryDisconnect(c);
return newPath;
} catch (Exception e) {
throw convertException(e);
}
}
示例10: mkdirs
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private void mkdirs(ChannelSftp chan, String dir) throws SftpException {
StringBuilder pathBuilder = new StringBuilder(dir.length());
for (String part : Splitter.on('/').omitEmptyStrings().split(dir)) {
pathBuilder.append(part);
chan.mkdir(pathBuilder.toString());
pathBuilder.append('/');
}
}
示例11: prepareUpload
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
public boolean prepareUpload(
ChannelSftp sftpChannel,
String path,
boolean overwrite)
throws SftpException, IOException, FileNotFoundException {
boolean result = false;
// Build romote path subfolders inclusive:
String[] folders = path.split("/");
for (String folder : folders) {
if (folder.length() > 0) {
// This is a valid folder:
try {
System.out.println("Current Folder path before cd:" + folder);
sftpChannel.cd(folder);
} catch (SftpException e) {
// No such folder yet:
System.out.println("Inside create folders: ");
sftpChannel.mkdir(folder);
sftpChannel.cd(folder);
}
}
}
// Folders ready. Remove such a file if exists:
if (sftpChannel.ls(path).size() > 0) {
if (!overwrite) {
System.out.println(
"Error - file " + path + " was not created on server. " +
"It already exists and overwriting is forbidden.");
} else {
// Delete file:
sftpChannel.ls(path); // Search file.
sftpChannel.rm(path); // Remove file.
result = true;
}
} else {
// No such file:
result = true;
}
return result;
}
示例12: processCommands
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private Object processCommands(ChannelSftp channel) throws Exception {
Object result = null;
if ("get".equals(action)) {
if (log.isDebugEnabled()) {
log.debug("Sftp get from '" + from + "' to '" + to + "'");
}
ensureFrom();
if (StringUtils.isBlank(to)) {
// return content as result
ByteArrayOutputStream out = new ByteArrayOutputStream();
channel.get(from, out);
result = out.toString("UTF-8");
} else {
channel.get(from, to);
}
doPostOperations(channel, from);
} else if ("put".equals(action)) {
if (log.isDebugEnabled()) {
log.debug("Sftp put from '" + from + "' to '" + to + "'");
}
ensureTo();
if (StringUtils.isBlank(from)) {
// put value as content
Object val = getValue();
if (val == null) {
throw new PaxmlRuntimeException("Sftp command wrong: no value to put on remote server");
}
InputStream in = new ByteArrayInputStream(String.valueOf(val).getBytes("UTF-8"));
channel.put(in, to);
} else {
channel.put(from, to);
}
doPostOperations(channel, to);
} else if ("move".equals(action)) {
if (log.isDebugEnabled()) {
log.debug("Sftp move from '" + from + "' to '" + to + "'");
}
ensureFrom();
ensureTo();
channel.rename(from, to);
} else if ("delete".equals(action)) {
if (log.isDebugEnabled()) {
log.debug("Sftp delete from: " + from);
}
ensureFrom();
channel.rm(from);
} else if ("mkdir".equals(action)) {
if (log.isDebugEnabled()) {
log.debug("Sftp mkdir to: " + to);
}
ensureTo();
channel.mkdir(to);
} else if ("list".equals(action)) {
if (log.isDebugEnabled()) {
log.debug("Sftp list from: " + from);
}
ensureFrom();
result = channel.ls(from);
} else {
throw new PaxmlRuntimeException("Unknown sftp action: " + action);
}
return result;
}
示例13: mkdirs
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@Override
public boolean mkdirs(Path path, FsPermission permission) throws IOException {
ChannelSftp channel = null;
try {
channel = fsHelper.getSftpChannel();
channel.mkdir(path.toString());
channel.chmod((int) permission.toShort(), path.toString());
} catch (SftpException e) {
throw new IOException(e);
} finally {
safeDisconnect(channel);
}
return true;
}
示例14: _ensurePath
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private void _ensurePath(ChannelSftp channel, FTPClient ftpClient, String path, boolean excludeLast) throws Exception {
String buildPath = "";
if (path.substring(0, 1).equals("/")) {
path = path.substring(1);
buildPath += "/";
}
String[] pathSegments = path.split("/");
int size = pathSegments.length;
if (excludeLast) {
size -= 1;
}
for (int i=0; i<size; i++) {
buildPath += pathSegments[i] + "/";
if (channel != null) {
try {
channel.stat(buildPath);
} catch (Exception e) {
channel.mkdir(buildPath);
}
} else {
ftpClient.makeDirectory(buildPath);
ftpClient.site("CHMOD 755 " + buildPath);
}
}
}
示例15: mkdirs
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private void mkdirs(String directory, ChannelSftp c) throws SftpException {
try {
SftpATTRS att = c.stat(directory);
if (att != null && att.isDir()) {
return;
}
} catch (SftpException ex) {
if (directory.indexOf('/') != -1) {
mkdirs(directory.substring(0, directory.lastIndexOf('/')), c);
}
c.mkdir(directory);
}
}