Java Code Examples for com.jcraft.jsch.ChannelSftp#LsEntry

The following examples show how to use com.jcraft.jsch.ChannelSftp#LsEntry . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SshFileController.java    From Jpom with MIT License 8 votes vote down vote up
/**
 * 删除文件或者文件夹
 *
 * @param channel channel
 * @param path    文件路径
 * @throws SftpException SftpException
 */
private void deleteFile(ChannelSftp channel, String path) throws SftpException {
    Vector<ChannelSftp.LsEntry> vector = channel.ls(path);
    if (null == vector) {
        return;
    }
    int size = vector.size();
    if (size == 1) {
        // 文件,直接删除
        channel.rm(path);
    } else if (size == 2) {
        // 空文件夹,直接删除
        channel.rmdir(path);
    } else {
        // 删除文件夹下所有文件
        String fileName;
        for (ChannelSftp.LsEntry en : vector) {
            fileName = en.getFilename();
            if (!".".equals(fileName) && !"..".equals(fileName)) {
                deleteFile(channel, path + "/" + fileName);
            }
        }
        channel.rmdir(path);
    }
}
 
Example 2
Source File: SftpClient.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized List<RemoteFile> listFiles() throws RemoteException {
    List<RemoteFile> result = null;
    String pwd = null;
    try {
        pwd = sftpClient.pwd();
        sftpLogger.info("LIST"); // NOI18N
        sftpLogger.info(NbBundle.getMessage(SftpClient.class, "LOG_DirListing"));

        @SuppressWarnings("unchecked")
        Collection<ChannelSftp.LsEntry> files = sftpClient.ls(pwd);
        result = new ArrayList<>(files.size());
        for (ChannelSftp.LsEntry entry : files) {
            result.add(new RemoteFileImpl(entry, pwd));
        }

        sftpLogger.info(NbBundle.getMessage(SftpClient.class, "LOG_DirectorySendOk"));
    } catch (SftpException ex) {
        LOGGER.log(Level.FINE, "Error while listing files for " + pwd, ex);
        sftpLogger.error(ex.getLocalizedMessage());
        throw new RemoteException(NbBundle.getMessage(SftpClient.class, "MSG_CannotListFiles", pwd), ex);
    }
    return result;
}
 
Example 3
Source File: SftpClient.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized int getPermissions(String path) throws RemoteException {
    int permissions = -1;
    try {
        sftpLogger.info("LIST " + path); // NOI18N
        sftpLogger.info(NbBundle.getMessage(SftpClient.class, "LOG_DirListing"));

        ChannelSftp.LsEntry file = getFile(path);
        if (file != null) {
            permissions = file.getAttrs().getPermissions();
        }

        sftpLogger.info(NbBundle.getMessage(SftpClient.class, "LOG_DirectorySendOk"));
    } catch (SftpException ex) {
        LOGGER.log(Level.FINE, "Error while getting permissions for " + path, ex);
    }
    return permissions;
}
 
Example 4
Source File: CommandDelegatorMethods.java    From jumbune with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * This method downloads whole directory recursively
 * @param channelSftp
 * @param remotePath remote directory (not file) path
 * @param localPath
 * @throws SftpException
 */
private void downloadDirectory(ChannelSftp channelSftp, String remotePath, String localPath, String user) throws SftpException {
	@SuppressWarnings("unchecked")
	Vector<ChannelSftp.LsEntry> childs = channelSftp.ls(remotePath);
	changeOwnership(localPath, user);
	for (ChannelSftp.LsEntry child : childs) {
		if (child.getAttrs().isDir()) {
			if (CURRENT_DIR.equals(child.getFilename()) || PARENT_DIR.equals(child.getFilename())) {
				continue;
			}
			new File(localPath + File.separator + child.getFilename()).mkdirs();
			changeOwnership(localPath + File.separator + child.getFilename(), user);
			downloadDirectory(channelSftp, remotePath + File.separator + child.getFilename() + File.separator,
					localPath + File.separator + child.getFilename(),user);
		} else {
			channelSftp.get(remotePath + File.separator + child.getFilename(),
					localPath + File.separator + child.getFilename());
			changeOwnership(localPath + File.separator + child.getFilename(), user);
		}
		
	}
}
 
Example 5
Source File: SftpClient.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private ChannelSftp.LsEntry getFile(String path) throws SftpException {
    assert Thread.holdsLock(this);
    assert path != null && path.trim().length() > 0;

    @SuppressWarnings("unchecked")
    List<ChannelSftp.LsEntry> files = sftpClient.ls(path);
    // in fact, the size of the list should be exactly 1
    LOGGER.fine(String.format("Exactly 1 file should be found for %s; found %d", path, files.size()));
    if (files.size() > 0) {
        return files.get(0);
    }
    return null;
}
 
Example 6
Source File: SftpClient.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public RemoteFileImpl(ChannelSftp.LsEntry entry, String parentDirectory) {
    assert entry != null;
    assert parentDirectory != null;
    this.entry = entry;
    this.parentDirectory = parentDirectory;
}
 
Example 7
Source File: CommandDelegatorMethods.java    From jumbune with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Checks if the remote file path provided is a directory or a file.
 * @param channelSftp
 * @param remotePath
 * @return
 * @throws SftpException
 */
@SuppressWarnings("unchecked")
private boolean isDirectory(ChannelSftp channelSftp, String remotePath) throws SftpException {
	Vector<ChannelSftp.LsEntry> childs = channelSftp.ls(remotePath);
	return childs.size() == 1 ? false : true;
}