1

I'm using ftp4j as FTP client.

FTPClient client = new FTPClient();
client.connect("86.22.11.178");
client.login("usr", "pwd");
client.changeDirectory("/dir");
client.upload(file);

It works fine at localhost, but it does not work when enclosed in a JSF web application deployed on a web server. I succeeded to do connect and login, when the code reaches to the upload command, it just skips on that and does nothing. No exception is been thrown.

There is full conectivity to the FTP server, it can't be a problem. I have also set chmod 777 permission on the files and they belong to the same owner.

This code worked on a Windows machine, could it be that machines running on Linux have different "rules"?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Shani
  • 17
  • 1
  • 3

1 Answers1

0

Your code seems to be correct. Try to find out the FTP error which its throws. Sometimes timeout may happens, which i faced!!!

import org.apache.commons.net.ftp.;
import java.io.
;

/** * This class is used to demonstrate the usage of the Jakarta Commons Net package */
public class TestFTP {

/** Creates a new instance of TestFTP */  
public TestFTP() {  
}  

/** 
 * main - Unit test program 
 * @param args Command line arguments 
 * 
 */  
public static void main(String args[]) {  
    try {  
        String ftpHost = "157.227.38.131";  
        String ftpUserName = "firebird";  
        String ftpPassword = "tcs@12345";  
        String ftpRemoteDirectory = "/etc/vlp/uploaded_files";  
        String fileToTransmit = "c:\\temp\\VLPDYN18022010174439.an";  

        //Create a Jakarta Commons Net FTP Client object  
        FTPClient ftp = new FTPClient();  

        //A datatype to store responses from the FTP server  
        int reply;  

        //  
        //Connect to the server  
        //  
        ftp.connect(ftpHost);  

        //  
        // After connection attempt, you should check the reply code to verify success.  
        //  
        reply = ftp.getReplyCode();      
        if(!FTPReply.isPositiveCompletion(reply)) {  
            try {  
                ftp.disconnect();  
            } catch (Exception e) {  
                System.err.println("Unable to disconnect from FTP server " +  
                                   "after server refused connection. "+e.toString());  
            }  
            throw new Exception ("FTP server refused connection.");  
        }                
        System.out.println("Connected to " + ftpHost + ". "+ftp.getReplyString());  

        //  
        //Try to login  
        //  
        if (!ftp.login(ftpUserName, ftpPassword)) {  
            throw new Exception ("Unable to login to FTP server " +  
                                 "using username "+ftpUserName+" " +  
                                 "and password "+ftpPassword);  
        }  

        System.out.println(ftp.getReplyString());  
        System.out.println("Remote system is " + ftp.getSystemName());  

        //  
        //Set our file transfer mode to either ASCII or Binary  
        //  
        //ftp.setFileType(FTP.ASCII_FILE_TYPE);  
        ftp.setFileType(FTP.BINARY_FILE_TYPE);  

        //  
        //Change the remote directory  
        //  
        if (ftpRemoteDirectory != null && ftpRemoteDirectory.trim().length() > 0) {  
            System.out.println("Changing to FTP remote dir: " + ftpRemoteDirectory);  
            ftp.changeWorkingDirectory(ftpRemoteDirectory);  
            reply = ftp.getReplyCode();  

            if(!FTPReply.isPositiveCompletion(reply)) {  
                throw new Exception ("Unable to change working directory " +  
                                     "to:"+ftpRemoteDirectory);  
            }  
        }  

        //  
        //Get the file that we will transfer and send it.  
        //  
        File f = new File(fileToTransmit);  
        System.out.println("Storing file as remote filename: " + f.getName());  
        boolean retValue=true;  
       try{   
        retValue = ftp.storeFile(f.getName(), new FileInputStream(f));  
       }catch(Exception e){e.printStackTrace();}  
        if (!retValue) {  
          throw new Exception ("Storing of remote file failed. ftp.storeFile() returned false.");  
        }  

        //Disconnect from the FTP server  
        //  
        try {  
            //ftp.logout();  
            ftp.disconnect();  
        } catch (Exception exc) {  
            System.err.println("Unable to disconnect from FTP server. " + exc.toString());  
        }  

    } catch (Exception e) {  
        System.err.println("Error: "+e.toString());  
    }  

    System.out.println("Process Complete.");  
    System.exit(0);  
}      

}

Hariharan
  • 3,191
  • 4
  • 19
  • 31