0

我正在尝试使用 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();
}
}

上述代码的输出是:

DONE
DONE2

这意味着一切正常,但我没有在代码中指定的目录中找到该文件。

请指教。

4

1 回答 1

0

如果您仍然需要帮助,我认为您应该尝试以这种方式调用 tftpClient.sendFile 方法:

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

在使用InetAddress.getByName(host)时,它应该通过 ip 字符串表示或主机名来确定您的主机 IP 地址,如此处所述。希望它以这种方式工作。

于 2013-11-13T12:43:50.783 回答