1

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.

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.

Javaeg
  • 11
  • 3
  • Are you sure that the file really exists? try to ask for the length of the file : System.out.println(file.length()); – WeMakeSoftware Sep 25 '12 at 15:09
  • the file exists on local machine ,and if the file wasn't exist i would get exception of type FilenotfoundException. – Javaeg Oct 03 '12 at 11:41

1 Answers1

0

If you still need help, I think you should try call tftpClient.sendFile method this way:

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. Hope it works this way.

  • Did you resolve the issue? I am also getting a similar error where I cannot send the file via HTTPClient so I am interesting in finding out if you resolved your issue and how? – jrobertsz66 May 13 '15 at 02:43