请求回复中的源端口(69)不正确。 TFTP sendFile

时间:2017-05-09 21:38:01

标签: java io ftp network-programming tftp

我想用Java中的TFTP Client执行这个TFTP DOS send命令:

tftp -i 192.168.1.13 put file1.romz file1.romz

这意味着以二进制方式将文件file1.romz发送到host: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随机使用端口在字符串的第一帧中发送数据。我想这就是我有这个错误信息的原因。你怎么能改变那个源端口?或者是导致此错误的另一个问题?

提前致谢。

1 个答案:

答案 0 :(得分:-1)

像解决方法一样。我用它来用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();
}