简体   繁体   English

无法使用TFTPClient发送文件(Apache Commons Net库)

[英]Can't Send File using TFTPClient (Apache Commons Net library)

I am trying to create TFTPClient using Apache Commons Net to put file on Server (AIX OS) and TFTP service is running on that Server, there isn't any exception raised while running the below code and it seems that everything is ok, but the file didn't put on the server. 我正在尝试使用Apache Commons Net创建TFTPClient并将文件放在服务器(AIX OS)上,并且TFTP服务正在该服务器上运行,在运行以下代码时没有引发任何异常,而且看起来一切正常,但是文件没有放在服务器上。

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import org.apache.commons.net.tftp.TFTP;
import org.apache.commons.net.tftp.TFTPClient;

public class Test {

/**
 * @param args
 * @throws IOException 
 * @throws SocketException 
 */

public static void main(String[] args) throws SocketException, IOException {
    int timeout=5000;
    String host="192.168.1.20";
    int port=22;

    TFTPClient tftpClient=new TFTPClient();

    tftpClient.setDefaultTimeout(60000);
    tftpClient.open(69);

    tftpClient.setSoTimeout(timeout);
    System.out.println("DONE");
    FileInputStream input = null;
    File file;

    file = new File("D:\\project.ear");
    input = new FileInputStream(file);
    try{
    tftpClient.sendFile("/home/dev/project.ear", TFTP.BINARY_MODE, input, host);

    }
    catch (UnknownHostException e)
    {
        System.err.println("Error: could not resolve hostname.");
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("DONE2");
    tftpClient.close();
}
}

the output of the above code was: 上面代码的输出是:

DONE
DONE2

which means that everything is OK but i didn't find the file in the directory specified in code. 这意味着一切都很好,但是我没有在代码中指定的目录中找到文件。

please advice. 请指教。

If you still need help, I think you should try call tftpClient.sendFile method this way: 如果您仍然需要帮助,我认为您应该尝试通过以下方式调用tftpClient.sendFile方法:

tftpClient.sendFile("/home/dev/project.ear", TFTP.BINARY_MODE, input, InetAddress.getByName(host));

While using InetAddress.getByName(host) it should determine your host ip address either by ip string representation or hostname, as it says here . 在使用InetAddress.getByName(host)时,它应该通过ip字符串表示形式或主机名来确定您的主机ip地址,如此处所示 Hope it works this way. 希望它能这样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Apache Commons Net写入远程文件? - How can I write to a remote file using Apache Commons Net? 使用 java 中的 Apache 共享网络库将文件上传到 ftp 服务器中的特定目录 - upload file to a specific directory in ftp server using Apache Commons Net library in java 我无法使用apache-commons-net TelnetClient禁用echo选项 - I can't disable echo option using apache-commons-net TelnetClient 如何使用apache commons net ftp更改ftp服务器上文件的权限? - How can I change permissions of a file on a ftp server using apache commons net ftp? 无法使用Apache Commons从Java的只读目录中删除文件 - Can't delete a file from a read-only directory in Java using Apache Commons 使用commons net 3.0 api(org.apache.commons.net)在ftp服务器上上传文件时出现问题 - Problem in Uploading file on ftp server using commons net 3.0 api (org.apache.commons.net ) 将Apache Commons Net库添加到Eclipse - Adding the Apache Commons Net library to Eclipse 使用Apache Commons文件上传 - Using Apache Commons File Upload 使用apache commons将文件存储到ftp服务器中不起作用 - store a file into a ftp server using apache commons doesn't work 无法使用 Apache Commons FTPClient 访问 FTP 服务器上的子文件夹 - Can't access subfolders on FTP server using Apache Commons FTPClient
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM