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 = ""; //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
      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
      ftp.closeServer(); //Close connection
   } catch (Exception e) {
      System.out.println("Error: " + e.getMessage());

Replies will be appriciated..

Thank in advance

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

This article has been dead for over six months. Start a new discussion instead.