Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3875110
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:13:44+00:00 2026-05-19T22:13:44+00:00

I am using the Ganymed API to sftp in to an Unix server .

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T22:13:45+00:00Added an answer on May 19, 2026 at 10:13 pm

    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

        /**
     * Write bytes to a file. If <code>len</code> &gt; 32768, then the write operation will
     * be split into multiple writes.
     * 
     * @param handle a SFTPv3FileHandle handle.
     * @param fileOffset offset (in bytes) in the file.
     * @param src the source byte array.
     * @param srcoff offset in the source byte array.
     * @param len how many bytes to write.
     * @throws IOException
     */
    public void write(SFTPv3FileHandle handle, long fileOffset, byte[] src, int srcoff, int len) throws IOException
    {
        checkHandleValidAndOpen(handle);
    
        if (len < 0)
    
            while (len > 0)
            {
    

    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 🙂

        public static void main(String[] args) throws Exception {
            Connection conn = new Connection(hostname);
            conn.connect();
            boolean isAuthenticated = conn.authenticateWithPassword(username, password);
    
            if (isAuthenticated == false)
                throw new IOException("Authentication failed.");
            SFTPv3Client client = new SFTPv3Client(conn);
            File tmpFile = File.createTempFile("teststackoverflow", "dat");
            FileWriter fw = new FileWriter(tmpFile);
            fw.write("this is a test");
            fw.flush();
            fw.close();
    
            SFTPv3FileHandle handle = client.createFile(tmpFile.getName());
            FileInputStream fis = new FileInputStream(tmpFile); 
            byte[] buffer = new byte[1024];
            int i=0;
                        long offset=0;
            while ((i = fis.read(buffer)) != -1) {
                client.write(handle,offset,buffer,0,i);
                                offset+= i;
    
            }
            client.closeFile(handle);
            if (handle.isClosed())  System.out.println("closed");;
            client.close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Eclipse Ganymede to create a web application, but the project's currently just
currently I am trying to run some commands on a ssh-server using the Ganymed-SSH2-Library
I am using ganymed SSH2 java library to connect to a server using public
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using SQL Server 2008 R2 we are looking for a way to select the
Using the Redis info command, I am able to get all the stats of
Using Location.getBearing(); I seem to get randomly changing bearings. Aka, I can turn the
Using System.Diagnostics.EventLog .NET type one can programmatically create logs into the Event Viewer application.
I need to create an ODE server inside my Eclipse workspace, in the tutorial
I'm using Ganymede ssh2 to connect to server from my Java app and do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.