簡體   English   中英

無法使用TFTPClient發送文件(Apache Commons Net庫)

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

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

這意味着一切都很好,但是我沒有在代碼中指定的目錄中找到文件。

請指教。

如果您仍然需要幫助,我認為您應該嘗試通過以下方式調用tftpClient.sendFile方法:

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

在使用InetAddress.getByName(host)時,它應該通過ip字符串表示形式或主機名來確定您的主機ip地址,如此處所示 希望它能這樣工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM