2011-02-04 9 views
4

私はGanymed APIを使用してUnixサーバーにsftp接続しています。私はサーバーにファイルを作成することができますが、ファイルの内容は常に空です。Ganymed API:SFTPを使用して

GanymedのAPIの場所: http://www.ganymed.ethz.ch/ssh2/

コード:

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 

私はここに

+0

はあなたが別のSFTPクライアントとのことをテストすることによって、このサーバーにファイルをアップロードできることを確認したことがありますか?多分あなたのアカウントにはファイルを作成する権限はありますが、書き込むことはできません。 – gutch

答えて

6

を何か間違ったことをやっている場合、誰かが私に言うことができます私はあなたの問題を解決しようとした、と私はで終わりました同じ状況では、作成されたファイルは空のままです。

しかし、私は問題の原因を見つけたと思う。ここで

がお書きにデータを送信するときは、参照ganymed APIのch.ethz.ssh2.SFTPv3Client.write()メソッド

/** 
* 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) 
     { 

の抽出物である、lenは> 0である、との理由偽の条件は、メソッドはすぐに戻り、whileループに入りません(実際にファイルに何かを書き込む)。

私は文が右に「もし(0 < LEN)」の前に、誰かがそれを離れて取り、コードの無駄な部分を私たちに残さが...

を更新した後があったと思います

最新バージョンを入手してください(上記の例ではビルド210を使用していました)。 ビルド250と251に問題はありませんでした。

ここに私のコードがあり、それは私のsshサーバの新しいファイルに正しく書いています。

あなたがこれを防弾する必要があります:)

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(); 
} 
+1

新しいリリースはここにあります:http://www.cleondris.ch/ssh2/ – Tony

+0

ありがとうございました。私のコードでは...あなたは多くの時間を節約しました.Lessonは学んだ:APIコードを見なければならないでしょう...コードもありがとう、私はそれをテストし、うまくいけば教えてくれます..ありがとう!! ! – Maximus

+0

同じコードが完璧に機能します。ちょうどバージョンを変更する必要があった...ありがとうトン! – Maximus

関連する問題