KpyM Telnet/SSH Server - Forum
KTS SFTP w/ Ganymed SSH-2: uploads work, but files are empty
Derek Moore KTS SFTP w/ Ganymed SSH-2: uploads work, but files are empty
 
Kroum,

I'm attempting to use KTS' SFTP functionality to transfer files to/from Win2k8 R2 servers.

I'm coding an automated client with Ganymed SSH-2 for Java.

My sftp_get function works fine, I can download remote files from the Win2k8 R2 servers.

My sftp_put function appears to work, it creates a remote file, the upload of data seems to take place (the read/write loop works properly until EOF), but the resultant files are empty.

Any ideas? I've been digging through the KTS code, but I haven't found anything glaring just yet.


Derek Moore
 
The problem was with the old old Ganymed version I was using. I upgraded to the latest jar available at <http://www.cleondris.ch/opensource/ssh2/>, and my upload logic works perfectly now!

Code:

package org.hackunix.helpers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.SFTPException;
import ch.ethz.ssh2.SFTPv3Client;
import ch.ethz.ssh2.SFTPv3FileAttributes;
import ch.ethz.ssh2.SFTPv3FileHandle;

public class SshHelper
{

    public static void sftp_get(String host, String user, String pass, String rpath, String lpath) throws IOException
    {
        File rfile = new File(rpath);
        File lfile = new File(lpath);
        if (lfile.exists() && lfile.isDirectory())
        {
            lfile = new File(lfile, rfile.getName());
        }

        Connection ssh = new Connection(host);
        ssh.connect();
        ssh.authenticateWithPassword(user, pass);
        SFTPv3Client sftp = new SFTPv3Client(ssh);
        SFTPv3FileHandle file = sftp.openFileRO(rpath);

        long fileOffset = 0;
        byte[] dst = new byte[32768];
        int i = 0;
        FileOutputStream output = new FileOutputStream(lfile);
        while ((i = sftp.read(file, fileOffset, dst, 0, dst.length)) != -1)
        {
            fileOffset += i;
            output.write(dst, 0, i);
        }

        output.close();
        sftp.closeFile(file);
        sftp.close();
    }

    public static void sftp_put(String host, String user, String pass, String lpath, String rpath) throws IOException
    {
        File rfile = new File(rpath);
        File lfile = new File(lpath);
        if (!lfile.exists() || lfile.isDirectory())
        {
            throw new IOException("Local file must be a regular file: " + lpath);
        }

        Connection ssh = new Connection(host);
        ssh.connect();
        ssh.authenticateWithPassword(user, pass);
        SFTPv3Client sftp = new SFTPv3Client(ssh);

        try
        {
            SFTPv3FileAttributes attr = sftp.lstat(rpath);
            if (attr.isDirectory())
            {
                rfile = new File(rpath, lfile.getName());
            }
        }
        catch (SFTPException e)
        {
            try
            {
                SFTPv3FileAttributes attr = sftp.lstat(rfile.getParent());
                if (!attr.isDirectory())
                {
                    throw new IOException("Remote file's parent must be a directory: " + rpath, e);
                }
            }
            catch (SFTPException ex)
            {
                throw new IOException("Remote file's parent director must exist: " + rpath, ex);
            }
        }
        SFTPv3FileHandle file = sftp.createFileTruncate(rfile.getCanonicalPath());

        long fileOffset = 0;
        byte[] src = new byte[32768];
        int i = 0;
        FileInputStream input = new FileInputStream(lfile);
        while ((i = input.read(src)) != -1)
        {
            sftp.write(file, fileOffset, src, 0, i);
            fileOffset += i;
        }

        input.close();
        sftp.closeFile(file);
        sftp.close();
    }

    public static void scp_get(String host, String user, String pass, String rpath, String lpath) throws IOException
    {
        Connection ssh = new Connection(host);
        ssh.connect();
        ssh.authenticateWithPassword(user, pass);
        SCPClient scp = ssh.createSCPClient();
        scp.get(rpath, lpath);
    }

    public static void scp_get(String host, String user, String pass, String[] rpath, String lpath) throws IOException
    {
        Connection ssh = new Connection(host);
        ssh.connect();
        ssh.authenticateWithPassword(user, pass);
        SCPClient scp = ssh.createSCPClient();
        scp.get(rpath, lpath);
    }

    public static void scp_put(String host, String user, String pass, String lpath, String rpath) throws IOException
    {
        Connection ssh = new Connection(host);
        ssh.connect();
        ssh.authenticateWithPassword(user, pass);
        SCPClient scp = ssh.createSCPClient();
        scp.put(lpath, rpath);
    }

    public static void scp_put(String host, String user, String pass, String[] lpath, String rpath) throws IOException
    {
        Connection ssh = new Connection(host);
        ssh.connect();
        ssh.authenticateWithPassword(user, pass);
        SCPClient scp = ssh.createSCPClient();
        scp.put(lpath, rpath);
    }

}



 

© 2007 - 2008 Kroum Grigorov
Powered by phpBB © 2001, 2005 phpBB Group