com.sshtools.j2ssh.sftp.SftpFile Java Examples

The following examples show how to use com.sshtools.j2ssh.sftp.SftpFile. 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: SSHProxyForwarder.java    From swift-k with Apache License 2.0 6 votes vote down vote up
private void cleanupOldProxies(SftpSubsystemClient sftp, String globusDir)
        throws IOException {
    List<SftpFile> files = new ArrayList<SftpFile>();
    SftpFile dir = sftp.openDirectory(globusDir);
    try {
        sftp.listChildren(dir, files);
        long nowSecs = System.currentTimeMillis() / 1000;
        for (SftpFile f : files) {
            Matcher m = PROXY_NAME_PATTERN.matcher(f.getFilename());
            if (m.matches()) {
                long expirationTime = Long.parseLong(m.group(3));
                if (expirationTime < nowSecs) {
                    sftp.removeFile(globusDir + "/" + f.getFilename());
                }
            }
        }
    }
    finally {
        dir.close();
    }
}
 
Example #2
Source File: SSHUtils.java    From stevia with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Put file to remote host.
 *
 * @param host the hostname of the client
 * @param port the port number the user want to use for connection
 * @param username the username required for authentication
 * @param password the password required for authentication
 * @param localfilePath the localfile path where the file is situated
 * @param remotePath the remote path where the file is going to be put
 * @param fileName the file name you want to put
 * @throws IOException Signals that an I/O exception has occurred.
 */
@SuppressWarnings("unchecked")
public static void putFileToRemoteHost(String host, int port,String username,String password, String localfilePath,String remotePath,String fileName)throws IOException {
	SftpClient sftp = getSftpClient(host,port, username, password);
	sftp.lcd(localfilePath);
	sftp.cd(remotePath);
	sftp.put(fileName);
	List<SftpFile> dirContents = sftp.ls(remotePath);
    Iterator<SftpFile> it = dirContents.iterator();
    while(it.hasNext()){    		  
    	if(it.next().getFilename().equals(fileName)){
    		 SSH_LOG.info("The file " + fileName + " was tranferred successfully to " + remotePath);
    		 return;
    	}
    }
    SSH_LOG.error("The file " + fileName + " was not tranferred  to " + remotePath);	
}
 
Example #3
Source File: FtpSession.java    From iaf with Apache License 2.0 6 votes vote down vote up
public List<String> ls(String remoteDirectory, boolean filesOnly, boolean closeAfterSend) throws Exception {
	openClient(remoteDirectory);

	try {
		if (ftpType == SFTP) {
			List<String> result = new LinkedList<String>();
			List<?> listOfSftpFiles = sftpClient.ls();
			for (Iterator<?> sftpFileIt = listOfSftpFiles.iterator(); sftpFileIt.hasNext();) {
				SftpFile file = (SftpFile)sftpFileIt.next();
				String filename = file.getFilename();
				if (filesOnly || (! file.isDirectory())) {
					if (! filename.startsWith(".")) {
						result.add(filename);
					}
				}
			}
			return result;
		}
		return FileUtils.getListFromNames(ftpClient.listNames());
	}
	finally {
		if (closeAfterSend) {
			closeClient();
		}
	}
}
 
Example #4
Source File: SSHProxyForwarder.java    From swift-k with Apache License 2.0 5 votes vote down vote up
private SftpFile createFile(SftpSubsystemClient sftp, String dir, String name) throws IOException {
    FileAttributes fa = new FileAttributes();
    fa.setPermissions(new UnsignedInteger32(FileAttributes.S_IRUSR
            | FileAttributes.S_IWUSR));
    SftpFile f = sftp.openFile(dir + "/" + name,
            SftpSubsystemClient.OPEN_WRITE
                    | SftpSubsystemClient.OPEN_CREATE
                    | SftpSubsystemClient.OPEN_EXCLUSIVE,
            fa);
    // specifying the permissions to sftp.openFile() above doesn't
    // seem to work
    sftp.changePermissions(f, fa.getPermissions().intValue());
    return f;
}
 
Example #5
Source File: FileResourceImpl.java    From swift-k with Apache License 2.0 5 votes vote down vote up
public Collection<GridFile> list(String directory) throws FileResourceException {
    try {
        String absPath = absPath(directory);
        SftpFile f = new SftpFile(absPath, sftp.getAttributes(absPath));
        List<?> l = new ArrayList<Object>();
        sftp.listChildren(f, l);
        return translateList(l);
    }
    catch (Exception e) {
        throw translateException("Cannot list contents of " + directory, e);
    }
}
 
Example #6
Source File: FileResourceImpl.java    From swift-k with Apache License 2.0 5 votes vote down vote up
protected List<GridFile> translateList(List<?> l) {
    List<GridFile> t = new ArrayList<GridFile>(l.size());
    for (Object o : l) {
        SftpFile f = (SftpFile) o;
        GridFile g = new GridFileImpl();
        g.setAbsolutePathName(f.getAbsolutePath());
        g.setName(f.getFilename());
        setAttributes(g, f.getAttributes());
        t.add(g);
    }
    return t;
}
 
Example #7
Source File: FileResourceImpl.java    From swift-k with Apache License 2.0 5 votes vote down vote up
public void getFile(FileFragment remote, FileFragment local,
        final ProgressMonitor progressMonitor) throws FileResourceException {
    checkNoPartialTransfers(remote, local, "ssh");
    
    File localFile = new File(local.getFile());
    try {
        SftpFile file = sftp.openFile(absPath(remote.getFile()),
                SftpSubsystemClient.OPEN_READ);
        long total = file.getAttributes().getSize().longValue();
        byte[] buffer = new byte[65535];
        BufferedInputStream in = new BufferedInputStream(
                new SftpFileInputStream(file));
        BufferedOutputStream out = new BufferedOutputStream(
                new FileOutputStream(localFile));
        int read;
        long crt = 0;

        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
            crt += read;
            if (progressMonitor != null) {
                progressMonitor.progress(total, crt);
            }
        }
        in.close();
        out.close();
    }
    catch (Exception e) {
        throw translateException("Cannot transfer \"" + remote.getFile()
                + "\" to \"" + local.getFile() + "\"", e);
    }
}
 
Example #8
Source File: FileResourceImpl.java    From swift-k with Apache License 2.0 4 votes vote down vote up
/** Copy a local file to a remote file. Default option 'overwrite' */
public void putFile(FileFragment local, FileFragment remote,
        final ProgressMonitor progressMonitor) throws FileResourceException {
    if (local.isFragment() || remote.isFragment()) {
        throw new UnsupportedOperationException("The SSH provider does not support partial transfers");
    }
    File localFile = new File(local.getFile());
    try {
        FileAttributes attrs = new FileAttributes();
        // Open with rw as setting all permissiosn does not work untill we
        // have created the file
        //
        attrs.setPermissions("rw");

        SftpFile file = sftp.openFile(absPath(remote.getFile()),
                SftpSubsystemClient.OPEN_WRITE
                        | SftpSubsystemClient.OPEN_CREATE
                        | SftpSubsystemClient.OPEN_TRUNCATE, attrs);

        byte[] buffer = new byte[65535];
        BufferedInputStream in = new BufferedInputStream(
                new FileInputStream(localFile));
        BufferedOutputStream out = new BufferedOutputStream(
                new SftpFileOutputStream(file));
        int read;
        long total = localFile.length();
        long crt = 0;

        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
            crt += read;
            if (progressMonitor != null) {
                progressMonitor.progress(total, crt);
            }
        }

        in.close();
        out.close();
    }
    catch (Exception e) {
        throw translateException("Cannot transfer \"" + local.getFile()
                + "\" to \"" + remote.getFile() + "\"", e);
    }
}