com.jcraft.jsch.ChannelSftp Java Examples

The following examples show how to use com.jcraft.jsch.ChannelSftp. 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: SFTPRepository.java    From ant-ivy with Apache License 2.0 9 votes vote down vote up
@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;
}
 
Example #2
Source File: SFTPTransfer.java    From localization_nifi with Apache License 2.0 7 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public FileInfo getRemoteFileInfo(final FlowFile flowFile, final String path, String filename) throws IOException {
    final ChannelSftp sftp = getChannel(flowFile);
    final String fullPath;

    if (path == null) {
        fullPath = filename;
        int slashpos = filename.lastIndexOf('/');
        if (slashpos >= 0 && !filename.endsWith("/")) {
            filename = filename.substring(slashpos + 1);
        }
    } else {
        fullPath = path + "/" + filename;
    }

    final Vector<LsEntry> vector;
    try {
        vector = sftp.ls(fullPath);
    } catch (final SftpException e) {
        // ls throws exception if filename is not present
        if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
            return null;
        } else {
            throw new IOException("Failed to obtain file listing for " + fullPath, e);
        }
    }

    LsEntry matchingEntry = null;
    for (final LsEntry entry : vector) {
        if (entry.getFilename().equalsIgnoreCase(filename)) {
            matchingEntry = entry;
            break;
        }
    }

    return newFileInfo(matchingEntry, path);
}
 
Example #3
Source File: SFtpClientUtils.java    From bamboobsc with Apache License 2.0 7 votes vote down vote up
public static void rm(String user, String password, String addr, int port, List<String> fileName) throws JSchException, SftpException, Exception {
	
	if (fileName==null || fileName.size()<1) {
		return;
	}
	Session session = getSession(user, password, addr, port);				
	Channel channel = session.openChannel("sftp");
	channel.connect();
	ChannelSftp sftpChannel = (ChannelSftp) channel;		
	try {
		for (String f : fileName) {
			sftpChannel.rm(f);				
			logger.warn("success remove file from " + addr + " :" + fileName);				
		}
	} catch (Exception e) {
		e.printStackTrace();
		throw e;
	} finally {
		sftpChannel.exit();
		channel.disconnect();
		session.disconnect();				
	}		
}
 
Example #4
Source File: PooledSFTPConnection.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public boolean isOpen() {
	if (!session.isConnected()) {
		return false;
	}
	try {
		ChannelSftp channel = getChannelSftp();
		if (!channel.isConnected() || channel.isClosed()) {
			return false;
		}
		// test connection
		channel.realpath("."); // pwd() is cached, don't use
		return true;
	} catch (Exception ex) {
		return false;
	}
}
 
Example #5
Source File: SFTPTransfer.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void rename(final String source, final String target) throws IOException {
    final ChannelSftp sftp = getChannel(null);
    try {
        sftp.rename(source, target);
    } catch (final SftpException e) {
        switch (e.id) {
            case ChannelSftp.SSH_FX_NO_SUCH_FILE:
                throw new FileNotFoundException();
            case ChannelSftp.SSH_FX_PERMISSION_DENIED:
                throw new PermissionDeniedException("Could not rename remote file " + source + " to " + target + " due to insufficient permissions");
            default:
                throw new IOException(e);
        }
    }
}
 
Example #6
Source File: PooledSFTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void delete(ChannelSftp channel, Info info, DeleteParameters params) throws IOException, SftpException {
	if (Thread.currentThread().isInterrupted()) {
		throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
	}
	URI uri = info.getURI();
	if (info.isDirectory()) {
		if (params.isRecursive()) {
			List<Info> children = list(uri, channel, LIST_NONRECURSIVE);
			for (Info child: children) {
				delete(channel, child, params);
			}
			channel.rmdir(getPath(uri));
		} else {
			throw new IOException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.cannot_remove_directory"), uri)); //$NON-NLS-1$
		}
	} else {
		channel.rm(getPath(uri));
	}
}
 
Example #7
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 #8
Source File: SftpResourceUploader.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void upload(Factory<InputStream> sourceFactory, Long contentLength, URI destination) throws IOException {
    LockableSftpClient client = sftpClientFactory.createSftpClient(destination, credentials);

    try {
        ChannelSftp channel = client.getSftpClient();
        ensureParentDirectoryExists(channel, destination);
        InputStream sourceStream = sourceFactory.create();
        try {
            channel.put(sourceStream, destination.getPath());
        } finally {
            sourceStream.close();
        }
    } catch (com.jcraft.jsch.SftpException e) {
        throw new SftpException(String.format("Could not write to resource '%s'.", destination), e);
    } finally {
        sftpClientFactory.releaseSftpClient(client);
    }
}
 
Example #9
Source File: SFTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void delete(ChannelSftp channel, Info info, DeleteParameters params) throws IOException, SftpException {
	if (Thread.currentThread().isInterrupted()) {
		throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
	}
	URI uri = info.getURI();
	if (info.isDirectory()) {
		if (params.isRecursive()) {
			List<Info> children = list(uri, channel, LIST_NONRECURSIVE);
			for (Info child: children) {
				delete(channel, child, params);
			}
			channel.rmdir(getPath(uri));
		} else {
			throw new IOException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.cannot_remove_directory"), uri)); //$NON-NLS-1$
		}
	} else {
		channel.rm(getPath(uri));
	}
}
 
Example #10
Source File: SftpFileUtil.java    From Leo with Apache License 2.0 6 votes vote down vote up
/**
  * 删除指定目录,此目录必须为空的目录
  * @param directory
  * @return boolean
  */
 public  boolean delDir(String directory) {
	boolean flag=false;
	ChannelSftp channel=getChannel();
	try {
		channel.rmdir(directory);
		log.info("删除目录:"+directory+"成功");
		flag=true;
	} catch (SftpException e) {
		log.error("删除目录:"+directory+"失败");
		log.error(e.getMessage());
	}finally {
	//channel.quit();            
     }
	
	return flag;
}
 
Example #11
Source File: SftpCommandTests.java    From vividus with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings({ "PMD.ReplaceVectorWithList", "PMD.UseArrayListInsteadOfVector" })
void shouldExecuteLsCommand() throws IOException, SftpException
{
    ChannelSftp channel = mock(ChannelSftp.class);
    String path = "/dir-to-list";
    LsEntry lsEntry1 = mock(LsEntry.class);
    when(lsEntry1.getFilename()).thenReturn("file1.txt");
    LsEntry lsEntry2 = mock(LsEntry.class);
    when(lsEntry2.getFilename()).thenReturn("file2.story");
    Vector<LsEntry> ls = new Vector<>();
    ls.add(lsEntry1);
    ls.add(lsEntry2);
    when(channel.ls(path)).thenReturn(ls);
    String result = SftpCommand.LS.execute(channel, path);
    assertEquals("file1.txt,file2.story", result);
}
 
Example #12
Source File: SSHShell.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
/**
 * 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();
}
 
Example #13
Source File: SFtpClientUtils.java    From bamboobsc with Apache License 2.0 6 votes vote down vote up
@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;		
}
 
Example #14
Source File: SftpLightWeightFileSystem.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@Override
public boolean delete(Path path) throws IOException {
  ChannelSftp channel = null;
  try {
    channel = this.fsHelper.getSftpChannel();
    if (getFileStatus(path).isDirectory()) {
      channel.rmdir(HadoopUtils.toUriPath(path));
    } else {
      channel.rm(HadoopUtils.toUriPath(path));
    }
  } catch (SftpException e) {
    throw new IOException(e);
  } finally {
    safeDisconnect(channel);
  }
  return true;
}
 
Example #15
Source File: SftpLightWeightFileSystem.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@Override
public boolean rename(Path oldPath, Path newPath) throws IOException {
  ChannelSftp channelSftp = null;
  try {
    channelSftp = this.fsHelper.getSftpChannel();
    channelSftp.rename(HadoopUtils.toUriPath(oldPath), HadoopUtils.toUriPath(newPath));

  } catch (SftpException e) {
    throw new IOException(e);
  } finally {
    safeDisconnect(channelSftp);
  }
  return true;
}
 
Example #16
Source File: PooledSFTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void create(ChannelSftp channel, URI uri, CreateParameters params) throws IOException, SftpException {
	if (Thread.currentThread().isInterrupted()) {
		throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
	}
	boolean createDirectory = Boolean.TRUE.equals(params.isDirectory());
	boolean createParents = Boolean.TRUE.equals(params.isMakeParents());
	Info fileInfo = simpleInfo(uri, channel);
	String path = getPath(uri);
	Date lastModified = params.getLastModified();
	if (fileInfo == null) { // does not exist
		URI parentUri = URIUtils.getParentURI(uri);
		if (createParents) {
			create(channel, parentUri, params.clone().setDirectory(true));
		} else if (parentUri != null) {
			if (simpleInfo(parentUri, channel) == null) {
				throw new FileNotFoundException("No such directory: " + parentUri);
			}
		}
		if (createDirectory) {
			channel.mkdir(path);
		} else {
			createFile(channel, path);
		}
		if (lastModified != null) {
			setLastModified(channel, path, lastModified.getTime(), createDirectory);
		}
	} else {
		if (createDirectory != fileInfo.isDirectory()) {
			throw new IOException(MessageFormat.format(createDirectory ? FileOperationMessages.getString("IOperationHandler.exists_not_directory") : FileOperationMessages.getString("IOperationHandler.exists_not_file"), uri)); //$NON-NLS-1$ //$NON-NLS-2$
		}
		setLastModified(channel, path, (lastModified != null) ? lastModified.getTime() : System.currentTimeMillis(), fileInfo.isDirectory());
	}
}
 
Example #17
Source File: SFTPEnvironmentTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectChannelFull() throws IOException, JSchException {
    SFTPEnvironment env = new SFTPEnvironment();
    initializeFully(env);

    ChannelSftp channel = mock(ChannelSftp.class);

    env.connect(channel);

    verify(channel).connect((int) env.get("connectTimeout"));
    verifyNoMoreInteractions(channel);
}
 
Example #18
Source File: SFtpClientUtils.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
/**
 * 抓遠端檔案然後存到本機 , 多筆
 * 
 * @param user
 * @param password
 * @param addr
 * @param port
 * @param remoteFile
 * @param localFile
 * @throws JSchException
 * @throws SftpException
 * @throws Exception
 */
public static void get(String user, String password, String addr, int port, 
		List<String> remoteFile, List<String> localFile) throws JSchException, SftpException, Exception {
			
	Session session = getSession(user, password, addr, port);	
	Channel channel = session.openChannel("sftp");
	channel.connect();
	ChannelSftp sftpChannel = (ChannelSftp) channel;		
	try {
		for (int i=0; i<remoteFile.size(); i++) {
			String rf=remoteFile.get(i);
			String lf=localFile.get(i);
			logger.info("get remote file: " + rf + " write to:" + lf );
			sftpChannel.get(rf, lf);
			File f=new File(lf);
			if (!f.exists()) {
				f=null;
				logger.error("get remote file:"+rf + " fail!");
				throw new Exception("get remote file:"+rf + " fail!");
			}
			f=null;
			logger.info("success write:" + lf);
		}
	} catch (Exception e) {
		e.printStackTrace();
		throw e;
	} finally {
		sftpChannel.exit();
		channel.disconnect();
		session.disconnect();				
	}
}
 
Example #19
Source File: SftpFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Renames the file.
 */
@Override
protected void doRename(final FileObject newFile) throws Exception {
    final ChannelSftp channel = getAbstractFileSystem().getChannel();
    try {
        final SftpFileObject newSftpFileObject = (SftpFileObject) FileObjectUtils.getAbstractFileObject(newFile);
        channel.rename(relPath, newSftpFileObject.relPath);
    } finally {
        getAbstractFileSystem().putChannel(channel);
    }
}
 
Example #20
Source File: SFTPEnvironmentTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testInitializeChannelPostConnectWithNulls() throws IOException {
    SFTPEnvironment env = new SFTPEnvironment();
    initializeWithNulls(env);

    ChannelSftp channel = mock(ChannelSftp.class);

    env.initializePostConnect(channel);

    verifyNoMoreInteractions(channel);
}
 
Example #21
Source File: SFTPEnvironmentTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testInitializeChannelPostConnectFull() throws IOException, SftpException {
    SFTPEnvironment env = new SFTPEnvironment();
    initializeFully(env);

    ChannelSftp channel = mock(ChannelSftp.class);

    env.initializePostConnect(channel);

    verify(channel).cd((String) env.get("defaultDir"));
    verifyNoMoreInteractions(channel);
}
 
Example #22
Source File: JSchSshSession.java    From nomulus with Apache License 2.0 5 votes vote down vote up
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('/');
  }
}
 
Example #23
Source File: SFTPEnvironmentTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testInitializeChannelPreConnectFull() throws IOException, SftpException {
    SFTPEnvironment env = new SFTPEnvironment();
    initializeFully(env);

    ChannelSftp channel = mock(ChannelSftp.class);

    env.initializePreConnect(channel);

    verify(channel).setAgentForwarding((boolean) env.get("agentForwarding"));
    verify(channel).setFilenameEncoding((String) env.get("filenameEncoding"));
    verifyNoMoreInteractions(channel);
}
 
Example #24
Source File: SFTPEnvironment.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
ChannelSftp connect(Session session) throws IOException {
    try {
        if (containsKey(CONNECT_TIMEOUT)) {
            int connectTimeout = FileSystemProviderSupport.getIntValue(this, CONNECT_TIMEOUT);
            session.connect(connectTimeout);
        } else {
            session.connect();
        }

        return (ChannelSftp) session.openChannel("sftp"); //$NON-NLS-1$
    } catch (JSchException e) {
        throw asFileSystemException(e);
    }
}
 
Example #25
Source File: DefaultFileSystemExceptionFactory.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * If the {@link SftpException#id id} of the {@link SftpException} is not {@link ChannelSftp#SSH_FX_NO_SUCH_FILE} or
 * {@link ChannelSftp#SSH_FX_PERMISSION_DENIED}, this default implementation does not return a {@link FileSystemException}, but a
 * {@link NotLinkException} instead.
 */
@Override
public FileSystemException createReadLinkException(String link, SftpException exception) {
    if (exception.id == ChannelSftp.SSH_FX_NO_SUCH_FILE || exception.id == ChannelSftp.SSH_FX_PERMISSION_DENIED) {
        return asFileSystemException(link, null, exception);
    }
    final FileSystemException result = new NotLinkException(link, null, exception.getMessage());
    result.initCause(exception);
    return result;
}
 
Example #26
Source File: SftpFsHelper.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * The method returns a new {@link ChannelSftp} without throwing an exception. Returns a null if any exception occurs
 * trying to get a new channel. The method exists for backward compatibility
 *
 * @deprecated use {@link #getSftpChannel()} instead.
 *
 * @return
 */
@Deprecated
public ChannelSftp getSftpConnection() {
  try {
    return this.getSftpChannel();
  } catch (SftpException e) {
    log.error("Failed to get new sftp channel", e);
    return null;
  }
}
 
Example #27
Source File: SFTPRepository.java    From ant-ivy with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #28
Source File: SshdSftpEnabledTest.java    From sshd-shell-spring-boot with Apache License 2.0 5 votes vote down vote up
@DirtiesContext
@Test
public void testSftp2() throws Exception {
    Session session = openSession(props.getShell().getUsername(), props.getShell().getPassword());
    ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
    sftp.connect();
    sftp.disconnect();
    session.disconnect();
    assertTrue(new File("target/sftp/admin").exists());
}
 
Example #29
Source File: SFTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void create(ChannelSftp channel, URI uri, CreateParameters params) throws IOException, SftpException {
	if (Thread.currentThread().isInterrupted()) {
		throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
	}
	boolean createDirectory = Boolean.TRUE.equals(params.isDirectory());
	boolean createParents = Boolean.TRUE.equals(params.isMakeParents());
	Info fileInfo = info(uri, channel);
	String path = getPath(uri);
	Date lastModified = params.getLastModified();
	if (fileInfo == null) { // does not exist
		if (createParents) {
			URI parentUri = URIUtils.getParentURI(uri);
			create(channel, parentUri, params.clone().setDirectory(true));
		}
		if (createDirectory) {
			channel.mkdir(path);
		} else {
			createFile(channel, path);
		}
		if (lastModified != null) {
			setLastModified(channel, path, lastModified.getTime());
		}
	} else {
		if (createDirectory != fileInfo.isDirectory()) {
			throw new IOException(MessageFormat.format(createDirectory ? FileOperationMessages.getString("IOperationHandler.exists_not_directory") : FileOperationMessages.getString("IOperationHandler.exists_not_file"), uri)); //$NON-NLS-1$ //$NON-NLS-2$
		}
		setLastModified(channel, path, lastModified != null ? lastModified.getTime() : System.currentTimeMillis());
	}
}
 
Example #30
Source File: SftpFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an output stream to write the file content to.
 */
@Override
protected OutputStream doGetOutputStream(final boolean bAppend) throws Exception {
    // TODO - Don't write the entire file into memory. Use the stream-based
    // methods on ChannelSftp once the work properly
    /*
     * final ChannelSftp channel = getAbstractFileSystem().getChannel(); return new SftpOutputStream(channel);
     */

    final ChannelSftp channel = getAbstractFileSystem().getChannel();
    return new SftpOutputStream(channel, channel.put(relPath, bAppend ? ChannelSftp.APPEND : ChannelSftp.OVERWRITE));
}