我正在尝试通过Java中的TFTP客户端执行此TFTP DOS发送命令:

tftp -i 192.168.1.13 put file1.romz file1.romz


这意味着以二进制方式将文件file1.romz发送到主机192.168.1.13,并在到达file1.romz时重命名该文件。关于TFTP DOS command

这是代码:

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 TFTPClientToSend {

public final static void main(String[] args) {
    boolean closed;
    int transferMode = TFTP.BINARY_MODE;
    String hostname, localFilename, remoteFilename;
    TFTPClient tftp;

    // Get host and file arguments
    hostname = "192.168.1.13";
    localFilename = "file1.romz";
    remoteFilename = "file1.romz";

    // Create our TFTP instance to handle the file transfer.
    tftp = new TFTPClient();

    // We want to timeout if a response takes longer than 60 seconds
    tftp.setDefaultTimeout(60000);

    // Open local socket
    try {
        tftp.open();
    } catch (SocketException e) {
        System.err.println("Error: could not open local UDP socket.");
        System.err.println(e.getMessage());
        System.exit(1);
    }

    // We haven't closed the local file yet.
    closed = false;

    // We're sending a file
    FileInputStream input = null;

    // Try to open local file for reading
    try {
        input = new FileInputStream(localFilename);
    } catch (IOException e) {
        tftp.close();
        System.err.println("Error: could not open local file for reading.");
        System.err.println(e.getMessage());
        System.exit(1);
    }

    // Try to send local file via TFTP
    try {
        tftp.sendFile(remoteFilename, transferMode, input, hostname, 69);
    } catch (UnknownHostException e) {
        System.err.println("Error: could not resolve hostname.");
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        System.err.println(
                "Error: I/O exception occurred while sending file.");
        System.err.println(e.getMessage());
        System.exit(1);
    } finally {
        // Close local socket and input file
        tftp.close();
        try {
            input.close();
            closed = true;
        } catch (IOException e) {
            closed = false;
            System.err.println("Error: error closing file.");
            System.err.println(e.getMessage());
        }
    }

    if (!closed) {
        System.exit(1);
    }
}
}


该字段是正确的,但是我收到以下错误消息:

Error: I/O exception occurred while sending file.
Incorrect source port (69) in request reply.


TFTP UDP端口正式为69。Windows防火墙已关闭,TFTP Lantronix Server已启用,可以通过TFTP更新新的固件。 Java随机使用端口在字符串的第一帧中发送数据。我认为这就是为什么我有此错误消息。您如何更改该源端口?还是导致此错误的另一个问题?

提前致谢。

最佳答案

像一个解决方法。我已使用它在Java中执行Windows DOS命令。

try {
    Process proceso = Runtime.getRuntime().exec("tftp -i 192.168.1.13 put
file1.romz file1.romz");
} catch (IOException e) {
    e.printStackTrace();
}

10-06 06:53