I am using the Ganymed API to sftp in to an Unix server . I am able to create the file in server, but the contents of the file is always empty.
Ganymed API location:
http://www.ganymed.ethz.ch/ssh2/
Code:
function (ByteArrayOutputStream reportBytes){
// reportBytes is a valid ByteArrayOutputStream
// if I write it to a file in to a local directory using reportBytes.writeTo(fout);
// I can see the contents */
byte byteArray[]=reportBytes.toByteArray();
SFTPv3FileHandle SFTPFILEHandle=sftpClient.createFileTruncate("test.txt");
//The file is created successfully and it is listed in unix
// The permissions of the file -rw-r--r-- 1 test.txt
sftpClient.write(SFTPFILEHandle, 0, byteArray, 0,byteArray.length );
//The above line doesnt seem to work, the file is always empty
}
/* write function definition is */
public void write(SFTPv3FileHandle handle, long fileOffset, byte[] src, int srcoff, int len) throws IOException
Can someone tell me if I am doing something wrong here
I tried to solve your problem, and I ended up in the same situation, the created file remains empty.
However, I think I found the cause of the problem.
Here is an extract of the ch.ethz.ssh2.SFTPv3Client.write() method of ganymed API
You see, when you send data to write, len is > 0, and because of the bogus condition, the method returns right away, and it never enters the while loop (that actually write something to the file).
I guess there was a statement right after the “if (len < 0)” before, but someone took it away and left us with useless piece of code…
Update :
Go get the latest version (The example above was using build 210).
I had no problem with the build 250 and 251.
Here’s my code, and it’s writing correctly to a new file on my ssh server.
you will need to bulletproof this 🙂