Results 1 to 5 of 5
  1. #1
    dankobanana is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default FTP client problems

    so, I'm pretty new here so excuse if I make a mistake posting. I'm trying to program a FTP client that logs and uploads files (up to 5 simultaneously) to a FTP server. I'm encountering problems and need help.
    this is still work in progress but I am encountering a problem. the client copies the contents of the file (for now I'm just trying to copy 1 file, hence the files[0]) but names it NULL (this is the problem).
    here is the code:

    Java Code:
    public class FTPClient {
    	
    	public static void main (String[] args){
    		
    		int i =0;
    		String userName="user";
    		String passWord="pass";
    		String server="127.0.0.1";
    		String[] files=null;
    		String delimiter = ";";
    		
    		while (i < args.length) {
    			if (i==0 & args[i].equals("-u")) userName=args[++i]; else
    			if (args[i].equals("-p")) passWord=args[++i]; else
    			if (args[i].equals("-server")) server=args[++i]; else
    			if (args[i].equals("-files")) files=args[++i].split(delimiter,5);
    			i++;
            }
    	
    		
    		Client f= new Client ();
    		 	f.setHost(server);
    		 	f.setUser(userName);
    		 	f.setPassword(passWord);
    		 	boolean connected=f.connect(); 
    		 	f.setRemoteFile("novo/u.txt");
    		 	if (connected){
    		 	  if (f.uploadFile(files[0]))
    		 	    // display the message of success if uploaded
    		 	  System.out.println(f.getLastSuccessMessage ());
    		 	  else
    		 	    System.out.println(f.getLastErrorMessage ());
    		 	  }
    		 	  else
    		 	  // Display any connection exception, if any
    		 	    System.out.println(f.getLastErrorMessage ()); 
     	
    		
    	}
    }
    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class Client
    {
    
      /** The URL connection object */
      private URLConnection m_client;
    
      /** The FTP host/server to be connected */
      private String host;
    
      /** The FTP user */
      private String user;
    
      /** The FTP user’s password */
      private String password;
    
      /** The remote file that needs to be uploaded */
      private String remoteFile;
    
      /** The previous error message triggered after a method is called */
      private String erMesg;
    
      /** The previous success message after any method is called */
      private String succMesg;
    
      public Client(){}
    
      /** Setter method for the FTP host/server */
      public void setHost (String host)
      {
        this.host = host;
      }
    
      /** Setter method for the FTP user */
      public void setUser (String user)
      {
        this.user = user;
      }
    
      /** Setter method for the FTP user’s password */
      public void setPassword (String p)
      {
        this.password = p;
      }
    
      /** Setter method for the remote file, this must include the sub-directory path relative
       to the user’s home directory, e.g you’e going to download a file that is within a sub directory
       called “sdir”, and the file is named “d.txt”, so you shall include the path as “sdir/d.txt”
      */
      public void setRemoteFile (String d)
      {
        this.remoteFile = d;
      }
    
      /** The method that returns the last message of success of any method call */
      public synchronized String getLastSuccessMessage()
      {
        if (succMesg==null ) return ""; return succMesg;
      }
    
      /** The method that returns the last message of error resulted from any exception of any method call */
      public synchronized String getLastErrorMessage()
      {
        if (erMesg==null ) return ""; return erMesg;
      }
    
      /** The method that handles file uploading, this method takes the absolute file path
       of a local file to be uploaded to the remote FTP server, and the remote file will then
       be transfered to the FTP server and saved as the relative path name specified in method setRemoteFile
       @param localfilename – the local absolute file name of the file in local hard drive that needs to
       FTP over
      */
      public synchronized boolean uploadFile (String localfilename)
      {
        try{
    
          InputStream is = new FileInputStream(localfilename);
          BufferedInputStream bis = new BufferedInputStream(is);
          OutputStream os =m_client.getOutputStream();
          BufferedOutputStream bos = new BufferedOutputStream(os);
          byte[] buffer = new byte[1024];
          int readCount;
    
          while( (readCount = bis.read(buffer)) > 0)
          {
                bos.write(buffer, 0, readCount);
          }
          bos.close();
    
          this.succMesg = "Uploaded!";
    
          return true;
        }
        catch(Exception ex)
        {
          StringWriter sw0= new StringWriter ();
          PrintWriter p0= new PrintWriter ( sw0, true );
          ex.printStackTrace ( p0 );
          erMesg = sw0.getBuffer().toString ();
    
          return false;
        }
      }
    
      /** The method that connects to the remote FTP server */
      public synchronized boolean connect()
      {
        try{
    
        URL url = new URL("ftp://"+user+":"+password+"@"+host+"/"+remoteFile+";type=i");
        m_client = url.openConnection();
    
        return true;
    
        }
        catch(Exception ex)
        {
          StringWriter sw0= new StringWriter ();
          PrintWriter p0= new PrintWriter ( sw0, true );
          ex.printStackTrace ( p0 );
          erMesg = sw0.getBuffer().toString ();
          return false;
        }
      }
      
    
    }
    I thank you in advance for your help!
    Last edited by dankobanana; 02-20-2012 at 01:13 AM. Reason: thanks added

  2. #2
    dankobanana is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: FTP client problems

    I forgot to mention I'm testing this by using FileZilla Server and "-u test -p test -server 127.0.0.1 -files /var/a.txt;/var/b.txt;/var/c.txt" console input to the program. I can verify the contents of the NULL named file by opening it with notepad and seeing its contents are the same as /var/a.txt (aka files[0] in the code)

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,570
    Rep Power
    23

    Default Re: FTP client problems

    the client copies the contents of the file (for now I'm just trying to copy 1 file, hence the files[0]) but names it NULL
    Where does the client get the name that it is using to name the file?

  4. #4
    dankobanana is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: FTP client problems

    line 26 in the first bit and line 112 in the 2nd one I guess.

    (because I'm a n00b) I'm reusing and adapting this code that I found (I omitted the download part because I do not need it and added my void main)
    http://www.ajaxapp.com/2009/02/21/a-...ad-and-upload/
    Last edited by dankobanana; 02-20-2012 at 02:03 AM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,570
    Rep Power
    23

    Default Re: FTP client problems

    Your line numbers don't make sense looking at the posted code.

    I was asking: Where is the code that creates the file and gives it a name? Where does that code get the name from?

Similar Threads

  1. Replies: 6
    Last Post: 04-04-2013, 10:36 PM
  2. problems trying to call an EJB's method from a Java Application Client
    By ahmedbj in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 01-15-2012, 04:20 PM
  3. Replies: 1
    Last Post: 10-05-2008, 03:12 PM
  4. Problems with client and server
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 06:07 AM
  5. Problems in running client class
    By ai_2007 in forum Advanced Java
    Replies: 0
    Last Post: 06-30-2007, 02:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •