2011-02-04 2 views
4

Ganymed API를 사용하여 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

여기에 잘못된 일을하고 있다면 누군가가 나에게, 나는에 종료 할 수 동일한 상황에서 생성 된 파일은 비어 있습니다.

그러나 문제의 원인을 찾은 것 같습니다. 여기

당신이 쓸 수있는 데이터를 보낼 때, 참조

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

은, 렌이> 0 인 ch.ethz.ssh2.SFTPv3Client.write의 추출물() ganymed의 API의 방법이며, 때문에 가짜 조건, 메서드는 즉시 반환하고 절대로 while 루프에 들어 가지 않습니다 (실제로 파일에 무언가를 씁니다).

:

는 내가 문 바로 "(0 < 렌)"전에,하지만 누군가가 그것을 멀리했다 코드의 쓸모없는 조각으로 우리를 떠났다가 ...

업데이트 후이 있었다 추측

최신 버전 가져 오기 (위의 예에서는 빌드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

고마워요 .. 물건을 디버깅하려고 정말 실망했습니다. 내 코드에서 ... 내 시간을 많이 절약 했어. 레슨은 배웠다. API 코드를 살펴 봐야 할 것이다 ... 코드도 주셔서 감사 드리며, 테스트 해보고 작동하는 경우 알려 드리겠습니다 .. 감사합니다! ! – Maximus

+0

동일한 코드가 완벽하게 작동합니다 .. 버전을 변경해야했습니다 ... 감사합니다. – Maximus