【问题标题】:Ganymed API: SFTP usingGanymed API:使用 SFTP
【发布时间】:2011-06-21 01:48:26
【问题描述】:

我正在使用 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

如果我在这里做错了什么,有人可以告诉我

【问题讨论】:

  • 您是否确认可以通过使用另一个 SFTP 客户端进行测试来将文件上传到此服务器?也许您的帐户有权创建文件但没有写入文件的权限...

标签: java ssh sftp


【解决方案1】:

上面托尼的回答,带有完整的课程和进口。您需要添加 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();
        }
        

    }

}

【讨论】:

    【解决方案2】:

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

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

    这里是 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

    更新:

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

    【讨论】:

    • 非常感谢..尝试在我的代码中调试东西真的很令人沮丧...您节省了我很多时间..经验教训:必须查看 API 代码...谢谢对于代码,我将对其进行测试,如果可行,请告诉您。谢谢!!!
    • 相同的代码完美运行..只需要更改版本...非常感谢!!
    • @Tony,我试过了,效果很好,但前提是文件大小小于缓冲区大小 (1024)。这可能会有所帮助:在 int i=0 之后,添加一个长偏移量=0。将写语句改为client.write(handle,offset,buffer,0,i);然后加一行offset+= i;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    相关资源
    最近更新 更多