English 中文(简体)
Ganymed API:SFTP使用
原标题:Ganymed API: SFTP using
  • 时间:2011-02-04 00:41:30
  •  标签:
  • java
  • ssh
  • sftp

我正在使用Ganymed API将sftp传入Unix服务器。我可以在服务器中创建文件,但文件的内容总是空的。

Ganymed API location: 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

有人能告诉我这里是否做错了什么吗

最佳回答

我试图解决你的问题,但我最终遇到了同样的情况,创建的文件仍然是空的。

然而,我想我找到了问题的原因。

下面是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循环(实际上是向文件中写入一些内容)。

我想之前在“if(len<;0)”后面有一个语句,但有人把它拿走了,给我们留下了一段无用的代码。。。

更新:

Go get the latest version (The example above was using build 210). I had no problem with the build 250 and 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();
}
问题回答

托尼的回答在上面,有完整的课堂和进口。您需要添加ganymed和jsch jar:

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

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SFTPv3Client;
import ch.ethz.ssh2.SFTPv3FileHandle;

public class GanyMedFTP {

    public static void main(String[] args) {
        Connection conn = new Connection("serverip");
        try {
            conn.connect();
            
            boolean isAuthenticated = conn.authenticateWithPassword("myusername", "mypassword");

            if (isAuthenticated == false)
                throw new IOException("Authentication failed.");
            SFTPv3Client client = new SFTPv3Client(conn);
            String fileName="OddyRoxxx.txt";
            File tmpFile = File.createTempFile(fileName, "dat");
             
            SFTPv3FileHandle handle = client.createFile(fileName);
            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();
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        

    }

}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签