java - 甘力美 API : SFTP using

标签 java ssh sftp

我正在使用 Ganymed API 通过 sftp 连接到 Unix 服务器。我能够在服务器中创建文件,但文件的内容始终为空。

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

如果我做错了什么,谁能告诉我

最佳答案

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

不过,我想我找到了问题的原因。

这是 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)”后面有一个语句,但有人把它拿走了,留给我们一段无用的代码...

更新:

获取最新版本(上面的示例使用的是 build 210)。 我对 build 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();
}

关于java - 甘力美 API : SFTP using,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4893431/

相关文章:

java - 在 Java 中模拟 SFTP、FTP、FTPS、本地文件系统服务器

java.lang.NoSuchFieldError : No static field MapAttrs_liteMode of type I in class Lcom/google/android/gms/R$styleable; 错误

git pull突然挂了

java - hibernate 4 - 如何替换 JDBCContext.registerSynchronizationIfPossible()

linux - 在 Linux 上,如何仅通过 SSH 连接为 session 共享脚本?

ssh - 带有自定义 “--files-from”参数的cron无法运行rsync

netbeans - 在 NetBeans 中使用 Vagrant 计算机作为 SFTP 连接

python - Paramiko 。按修改时间获取文件

java - 在 Jax-Ws 中使用 @Autowire 和 Spring 容器

java - 绘制图结构的算法?