本文整理汇总了Java中com.jcraft.jsch.ChannelSftp.ls方法的典型用法代码示例。如果您正苦于以下问题:Java ChannelSftp.ls方法的具体用法?Java ChannelSftp.ls怎么用?Java ChannelSftp.ls使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jcraft.jsch.ChannelSftp
的用法示例。
在下文中一共展示了ChannelSftp.ls方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getRemoteFileList
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static Vector<LsEntry> getRemoteFileList(String user, String password, String addr, int port, String cwd) throws JSchException,
SftpException, Exception {
Session session = getSession(user, password, addr, port);
Vector<LsEntry> lsVec=null;
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
try {
lsVec=(Vector<LsEntry>)sftpChannel.ls(cwd); //sftpChannel.lpwd()
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
sftpChannel.exit();
channel.disconnect();
session.disconnect();
}
return lsVec;
}
示例2: getDir
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private void getDir(final ChannelSftp channel,
final String remoteFile,
final File localFile) throws IOException, SftpException {
String pwd = remoteFile;
if (remoteFile.lastIndexOf('/') != -1) {
if (remoteFile.length() > 1) {
pwd = remoteFile.substring(0, remoteFile.lastIndexOf('/'));
}
}
channel.cd(pwd);
if (!localFile.exists()) {
localFile.mkdirs();
}
@SuppressWarnings("unchecked")
final List<ChannelSftp.LsEntry> files = channel.ls(remoteFile);
for (ChannelSftp.LsEntry le : files) {
final String name = le.getFilename();
if (le.getAttrs().isDir()) {
if (".".equals(name) || "..".equals(name)) {
continue;
}
getDir(channel,
channel.pwd() + "/" + name + "/",
new File(localFile, le.getFilename()));
} else {
getFile(channel, le, localFile);
}
}
channel.cd("..");
}
示例3: listFiles
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
* Lists directory files on remote server.
* @throws URISyntaxException
* @throws JSchException
* @throws SftpException
*/
private void listFiles() throws URISyntaxException, JSchException, SftpException {
JSch jsch = new JSch();
JSch.setLogger(new JschLogger());
setupSftpIdentity(jsch);
URI uri = new URI(sftpUrl);
Session session = jsch.getSession(sshLogin, uri.getHost(), 22);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
System.out.println("Connected to SFTP server");
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
Vector<LsEntry> directoryEntries = sftpChannel.ls(uri.getPath());
for (LsEntry file : directoryEntries) {
System.out.println(String.format("File - %s", file.getFilename()));
}
sftpChannel.exit();
session.disconnect();
}
示例4: listEntries
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private Vector<LsEntry> listEntries(final ChannelSftp channelSftp, final String path) throws SftpException {
final Vector<LsEntry> vector = new Vector<LsEntry>();
LsEntrySelector selector = new LsEntrySelector() {
public int select(LsEntry entry) {
final String filename = entry.getFilename();
if (filename.equals(".") || filename.equals("..")) {
return CONTINUE;
}
if (entry.getAttrs().isLink()) {
vector.addElement(entry);
}
else if (entry.getAttrs().isDir()) {
if (keepDirectory(filename)) {
vector.addElement(entry);
}
}
else {
if (keepFile(filename)) {
vector.addElement(entry);
}
}
return CONTINUE;
}
};
channelSftp.ls(path, selector);
return vector;
}
示例5: ls
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public List<String> ls(String path)
throws FileBasedHelperException {
try {
List<String> list = new ArrayList<String>();
ChannelSftp channel = getSftpChannel();
Vector<LsEntry> vector = channel.ls(path);
for (LsEntry entry : vector) {
list.add(entry.getFilename());
}
channel.disconnect();
return list;
} catch (SftpException e) {
throw new FileBasedHelperException("Cannot execute ls command on sftp connection", e);
}
}
示例6: 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;
}
示例7: 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);
}
}
}
}
示例8: traverse
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
/**
* Delete directory and its content recursively.
*
* @param channel
* open channel to SFTP server
* @param path
* path of the directory
* @throws SftpException
* when something went wrong
*/
@SuppressWarnings("unchecked")
private void traverse(ChannelSftp channel, String path)
throws SftpException {
SftpATTRS attrs = channel.stat(path);
if (attrs.isDir()) {
Vector<LsEntry> files = channel.ls(path);
if (files != null && files.size() > 0) {
Iterator<LsEntry> it = files.iterator();
while (it.hasNext()) {
LsEntry entry = it.next();
if ((!entry.getFilename().equals(".")) && (!entry.getFilename().equals(".."))) {
traverse(channel, path + "/" + entry.getFilename());
}
}
}
channel.rmdir(path);
} else {
channel.rm(path);
}
}
示例9: recursiveDelete
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private static void recursiveDelete(ChannelSftp sftp, String path)
throws SftpException, JSchException {
Vector<?> entries = sftp.ls(path);
for (Object object : entries) {
LsEntry entry = (LsEntry) object;
if (entry.getFilename().equals(".")
|| entry.getFilename().equals("..")) {
continue;
}
if (entry.getAttrs().isDir()) {
recursiveDelete(sftp, path + entry.getFilename() + "/");
} else {
sftp.rm(path + entry.getFilename());
}
}
sftp.rmdir(path);
}
示例10: list
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public List<String> list(String parent) throws IOException {
try {
ChannelSftp c = getSftpChannel(parent);
String path = getPath(parent);
Collection<LsEntry> r = c.ls(path);
if (r != null) {
if (!path.endsWith("/")) {
path = parent + "/";
}
List<String> result = new ArrayList<>();
for (LsEntry entry : r) {
if (".".equals(entry.getFilename()) || "..".equals(entry.getFilename())) {
continue;
}
result.add(path + entry.getFilename());
}
return result;
}
} catch (SftpException | URISyntaxException e) {
throw new IOException("Failed to return a listing for '" + parent + "'", e);
}
return null;
}
示例11: listFile
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
@Override
public FileInfo listFile(String relativePath, boolean closeSession) {
ChannelSftp sftp = null;
FileInfo fileInfo = null;
try {
// Get a reusable channel if the session is not auto closed.
sftp = (closeSession) ? openConnectedChannel() : openConnectedChannel(CHANNEL_1);
sftp.cd(basePath);
if (!relativePath.equals(".") && !relativePath.equals("..")) {
@SuppressWarnings("rawtypes")
Vector list = sftp.ls(relativePath);
for (Object object : list) {
LsEntry entry = (LsEntry)object;
if (!entry.getFilename().equals(".") && !entry.getFilename().equals("..")) {
long updateTime = entry.getAttrs().getMTime();
fileInfo = new FileInfo(relativePath, entry.getAttrs().isDir(), updateTime * 1000, entry.getAttrs().getSize());
}
}
}
return fileInfo;
} catch (Exception e) {
return null;
} finally {
if (closeSession) {
close();
}
}
}
示例12: listFiles
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private List<FileEntry> listFiles(String path, ChannelSftp c) throws Exception {
try {
List<FileEntry> res = new ArrayList<FileEntry>();
@SuppressWarnings("rawtypes")
java.util.Vector vv = c.ls(extractSessionPath(path));
if (vv != null) {
for (int ii = 0; ii < vv.size(); ii++) {
Object obj = vv.elementAt(ii);
if (obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
LsEntry lsEntry = (com.jcraft.jsch.ChannelSftp.LsEntry) obj;
if ((lsEntry.getFilename().equals("."))
||(lsEntry.getFilename().equals(".."))
)
continue;
FileEntry fileEntry = new FileEntry();
fileEntry.displayName = lsEntry.getFilename();
fileEntry.path = createFilePath(path, fileEntry.displayName);
SftpATTRS attrs = lsEntry.getAttrs();
setFromAttrs(fileEntry, attrs);
res.add(fileEntry);
}
}
}
return res;
} catch (Exception e) {
tryDisconnect(c);
throw convertException(e);
}
}
示例13: 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;
}
示例14: 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;
}
示例15: getSFTPMatchingPathes
import com.jcraft.jsch.ChannelSftp; //导入方法依赖的package包/类
private static List<String> getSFTPMatchingPathes(String path, String[] pathStream, ArrayList<String> arrayList, ChannelSftp fc, DateTime lastReadout, DateTimeFormatterBuilder dtfbuilder) {
int nextTokenPos = getPathTokens(path).length;
if (nextTokenPos == pathStream.length - 1) {
arrayList.add(path);
return arrayList;
}
String nextToken = pathStream[nextTokenPos];
String nextFolder = null;
try {
if (containsDateToken(nextToken)) {
Vector listDirectories = fc.ls(path);
for (Object folder : listDirectories) {
LsEntry currentFolder = (LsEntry) folder;
if (!matchDateString(currentFolder.getFilename(), nextToken)) {
continue;
}
DateTime folderTime = getFolderTime(path + currentFolder.getFilename() + "/", pathStream);
if (folderTime.isAfter(lastReadout)) {
nextFolder = currentFolder.getFilename();
getSFTPMatchingPathes(path + nextFolder + "/", pathStream, arrayList, fc, lastReadout, dtfbuilder);
}
// }
}
} else {
nextFolder = nextToken;
getSFTPMatchingPathes(path + nextFolder + "/", pathStream, arrayList, fc, lastReadout, dtfbuilder);
}
} catch (SftpException ex) {
org.apache.log4j.Logger.getLogger(DataSourceHelper.class).log(org.apache.log4j.Level.ERROR, "Cant find suitable files on the device");
}
return arrayList;
}