[英]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.