Question: Java program to ftp file using FTPClient

newone2011
newone2011 used Ask the Experts™
on
I have the following code to ftp a file to a remore server. How do I specify the local directory from where the file needs to be sent. I n eed to send it from
C;\LocalTest to C:\Test.


import org.apache.commons.net.ftp.FTPClient;
import java.io.FileInputStream;
import java.io.IOException;
........


FTPClient client = new FTPClient();
       
        FileInputStream fis = null;
   
        try {            
        client.connect(servername);
 
        client.login(user, pwd);
                   
        fis = new FileInputStream(filename);

//Remote Dir
      client.changeWorkingDirectory("C:/Test");
     
        client.listFiles();
      client.storeFile(filename, fis);
         
        client.logout();
     
        } catch (IOException e) {
         
        e.printStackTrace();
     
        }
              
        try {            
        if (fis != null) {                    
        fis.close();            
        }              
        client.disconnect();
        return true;
         
        } catch (IOException e) {              
        e.printStackTrace();
        
        }    
      return false;

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Awarded 2011
Awarded 2011
Commented:
You just specify fiull path of the input file:

   fis = new FileInputStream("C:\\LocalTest\\" + filename);
This may be an obvious answer, but why not just use the full path in the filename?  Like instead of "file1", it could be "c:\localtest\file1".

Author

Commented:
Let me try
Awarded 2011
Awarded 2011

Commented:
Normal (manual) FTP programs usually have "lcd" command which changes default local directory
(analogous to cd which changes default remote directory).

However looking at FTPClient API I could not find analog to changeWorkingDirectory(...) method,
which operates on the remote side.
Maybe they were thinking that in  the program that does not matter so much and you can have
the path specified in the code when you open the file, sa mentioned in the previous postings

Let's see, if it works for you. Should work as FileInputStream  allows to have the full path as a String argument
of the constructor.
Commented:
You can't probably change to c:\test on a remote FTP server.  It is very unlikely that the root that the remote server presents over FTP is actually the root of the c:\ drive.  It is probably a subfolder specifically intended to be used as the ftp root.

Author

Commented:
I added the complete file path as the filename. Now it does not give me error message about not finding the file, it even goes through the ftp successfully, however cannot find the file on the remote server now
Awarded 2011
Awarded 2011

Commented:
On the remote server it has nothing to do with the local - you have certain permission and visibility of folders on the remote server.
Can you connect to your renote server using normal ftp client(not your program) and make cd to the folder you want?

Author

Commented:
Yes, I am able to ftp to that server using ftp client, the ftp is setup in way that it automatically takes me to the dir:
C:\Test

The program shows file transfer succesful, however when I go this folder, I do not see the updated file.  It shows me a file with old timestamp.
Awarded 2011
Awarded 2011

Commented:
How do you check - telneting to the server?

Author

Commented:
I added this code :
 if (client.storeFile(filename, fis)){
          System.out.println("File Transferred");  
      }else {
            System.out.println("File Transfer failed");
            client.registerSpyStream(System.err);

      }

The file transfer is failing. The server name, userid, passwd are all correct.  Anything I can do to debug and see why this is failing. I am able to manually ftp from the server where the code is running to where the file is supposed to be sent.  
Any suggestions or way to find out what is causing ftp to fail ?
Awarded 2011
Awarded 2011

Commented:
Do you see any excepotion?
If not, are you sure that you print stAcktrace for all exceptions in your code?
There should be some exception if it does not wiork.
Try to upload file from your default client folder - withut any path attached to the name - although it is improbable tlhat that woiuld be the reasoin.

Commented:
Are you saying that C:\test is the root folder of your FTP server?  If so, from the client site this will appear as the folder "/"

In other words, if c:\test is the ftp root and the file that you want is c:\test\readme.txt then, from the point of view of the ftp CLIENT this file is named /readme.txt instead of c:\test\readme.txt

Author

Commented:
I printed the trace and all it gave me is:
java.lang.RuntimeException: Did not transfer file

Let me change to /readme.tx and see what happens.  Will update shortly
Awarded 2011
Awarded 2011

Commented:
This method changeWorkingDirectory should either return boolean or throw exceoption - check API and cgeck if it is boolean - thre should be away it cmmunicates suyccess or failure

Author

Commented:
Well that doe snot work. I get the following exception:
java.io.FileNotFoundException: \readme.txtIf I use the fullpath there is no issue:

ie
c:\localtest\readme.txt  (This is on the client server)
Now I need to transfer to remote server. The login is succesfull (userid/pwd is correct)
Just does not transfer. So here my code again

FTPClient client = new FTPClient();
       
        FileInputStream fis = null;
   
        try {            
        client.connect(servername);
 
        if (client.login(user, pwd)){
             
              System.out.println("Login success");
        }else {
             
              System.out.println("Login failure");
        }
         
//         Create an InputStream of the file to be uploaded
                       String filename = " c:\localtest\readme.txt ";        
        fis = new FileInputStream(filename);
           if (client.storeFile(filename, fis)){
          System.out.println("File Transferred");  
      }else {
            System.out.println("File Transfer failed");
            throw new RuntimeException("Did not transfer file");
   
      }
         
        client.logout();
     
        } catch (IOException e) {
         
        e.printStackTrace();
     
        }
              
        try {            
        if (fis != null) {                    
        fis.close();            
        }              
        client.disconnect();
        return true;
         
        } catch (IOException e) {              
        e.printStackTrace();
        
        }    
      return false;





Author

Commented:
Ok, yang as per your rec I added this code to check if working dir could be changed:
if(client.changeWorkingDirectory("C:/Test")){
          System.out.println("Changed working dir successfully");  
      }else {
            System.out.println("Working dir could not be changed");  
            
      }
     
It gave me failure and could not change the working dir.

Author

Commented:
also printed the current working dir and it gave me:
The current WORKING DIR is /

I am transferring files from windows to windows

Commented:
I think maybe my previous post was unclear...

The files and folders on the client side are: c:\folder\subfolder\file.dat

When the client "sees" files on the server side, they will see things only starting with the ftp root, so, if c:\folder was root, the client would see /subfolder/file.dat

... anyway, you said this worked in the windows native FTP client... that means the server is working OK and it also means that the file transfer was done in active mode because the native command line client can't do passive mode... I'm not sure if this client defaults to active or passive so lets make sure your code is using active mode also by calling client.enterLocalActiveMode() after connecting and before transferring the file.

Author

Commented:
ok,  let me change to Active mode and see.

Author

Commented:
Made no difference, is there a way to print the detailed error output, why the error is coming etc
The runtimeexception is not indicating much.
Awarded 2011
Awarded 2011

Commented:
So what if you do not changeWorkingDirectory - just send you file there after you login.
Then can you make telnet to the same server with the same userid and you should
see the same folder and check if the file went there?

Author

Commented:
Yes, not working for some reason.  Probably some permission issue, program is correct. Anyways I am also considering another approach without using FTPClient.  Also the server is Windows 2003, not sure if that is causing issue.
Will update in some time
Awarded 2011
Awarded 2011

Commented:
Strange; permissions should probably work the same for manual operation and from the program.

Author

Commented:
Partial

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial