Hello Everybody,

          I am having a doubt regarding how to upload a file to multiple machines or servers.
          I tried using with FTP but getting ConnectException:connection Refused



import sun.net.ftp.*;
import java.io.*;

public class SendFile {
 public static void main(String args[]) {
   String hostname = "172.14.3.23"; //Remote FTP server: Change this
   String username = "usernamee"; //Remote user name: Change this
   String password = "password"; //Remote user password: Change this
   String upfile = "test.txt"; //File to upload passed on command line
   String remdir = "/home/user"; //Remote directory for file upload
   FtpClient ftp = new FtpClient();
   try {
      ftp.openServer(hostname); //Connect to FTP server
      System.out.println("Connected..");
      ftp.login(username, password); //Login
      ftp.binary(); //Set to binary mode transfer
      ftp.cd(remdir); //Change to remote directory
      File file = new File(upfile);
      OutputStream out = ftp.put(file.getName()); //Start upload
      InputStream in = new FileInputStream(file);
      byte c[] = new byte[4096];
      int read = 0;
      while ((read = in.read(c)) != -1 ) {
         out.write(c, 0, read);
      } //Upload finished
      in.close();
      out.close();
      ftp.closeServer(); //Close connection
   } catch (Exception e) {
      System.out.println("Error: " + e.getMessage());
   }
 }
}

Replies will be appriciated..

Thank in advance

Recommended Answers

All 2 Replies

Have you tried another FTP program to the same site to see that you have the correct values?

You did put in a valid user name and password (lines 12,13) I assume?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.