Shaik Allabakash
Shaik Allabakash

Reputation: 35

apache.commons.net.ftp.FTPClient not uploading the file to the required folder

I'm using the following code to upload an xml file to the directory /home/domainname/public_html/guest in the server. However, the file is uploaded only to the location /home/domainname. It is not uploading to the child directories. Please advise.

FTPClient client = new FTPClient(); FileInputStream fis = null;

    try {
        client.connect(Util.getProductsXMLFTPServer());
        client.login(Util.getProductsXMLFTPUser(), Util.getProductsXMLFTPPassword());

        //
        // Create an InputStream of the file to be uploaded
        //

        fis = new FileInputStream(new File(Util.getProductsXMLFTPInputFilePath(), Util.getProductsXMLFTPOutputFileName()));
        client.changeWorkingDirectory(Util.getProductsXMLFTPUploadPath());


        client.storeFile(Util.getProductsXMLFTPOutputFileName(), fis);
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Upvotes: 0

Views: 8078

Answers (2)

user2392847
user2392847

Reputation: 11

FTPClient ftpClient = new FTPClient();
        try {
              System.out.println("before server connection");
            ftpClient.connect(server, port);
            System.out.println("before user name and passwod");
            ftpClient.login(user, pass);
            ftpClient.enterLocalActiveMode();

            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            System.out.println("connection sucess");


            // windows working fine
            File secondLocalFile = new File("/home/aims/archived_reports/tes_S_000000123/test.pdf");
//            String secondRemoteFile = "/archived_reports/PermanentRecord.pdf";

//linux
          //  File secondLocalFile = new File("/archived_reports/tes_S_000009123/test.pdf");

            String secondRemoteFile = "remotefilename.pdf";
            InputStream  inputStream = new FileInputStream(secondLocalFile);

            System.out.println("Start uploading second file");

                    ftpClient.changeWorkingDirectory("/reports");// home/ftp.test/reports folder


          System.out.println("Prasent Working Directory :"+ftpClient.printWorkingDirectory());


            OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
                 int returnCode = ftpClient.getReplyCode();
                 System.out.println(returnCode);
            byte[] bytesIn = new byte[4096];
            int read = 1;

            while ((read = inputStream.read(bytesIn)) != -1) {
                outputStream.write(bytesIn, 0, read);
            }

System.out.println();           
            inputStream.close();
            outputStream.close();
                     boolean completed = ftpClient.completePendingCommand();
            if (completed) {
                System.out.println("The second file is uploaded successfully.");
            }

Upvotes: 1

Paweł Wyrwiński
Paweł Wyrwiński

Reputation: 1443

I checked your code, it works. I've only changed file type declaration to binary, which may be not needed for XML files. Here's my complete code for reference:

package apachenet.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

public class App {
    public static void main( String[] args ) {
        FTPClient client = new FTPClient(); 
        FileInputStream fis = null;
        try {
            client.connect(/*Util.getProductsXMLFTPServer()*/"127.0.0.1");
            client.login(/*Util.getProductsXMLFTPUser()*/"pwyrwinski", 
                    /*Util.getProductsXMLFTPPassword()*/"secret");
            client.setFileType(FTP.BINARY_FILE_TYPE); // optional
            fis = new FileInputStream(
                    new File(/* Util.getProductsXMLFTPInputFilePath() */"/home/pwyrwinski", 
                            /* Util.getProductsXMLFTPOutputFileName() */"img.png"));
            client.changeWorkingDirectory(/*Util.getProductsXMLFTPUploadPath()*/ "someDir");


            client.storeFile(/*Util.getProductsXMLFTPOutputFileName()*/"img_bis.png", fis);
            client.logout();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

As you can see it's roughly the same as yours. Util class calls are replaced with raw data.

When I ran it, file /home/pwyrwinski/img.png was uploaded to {FTP_USER_ROOT}/someDir directory on ftp server with name changed to img_bis.png. I assume this is exactly what you wanted to achieve.

Let's go back to your problem.

  1. Try to check what is returned from Util.getProductsXMLFTPUploadPath() call. My guess is it's not what you're expecting - so debug it in your IDE or print it to the console.
  2. Check if path returned from Util.getProductsXMLFTPUploadPath() call starts with slash, it shouldn't.

UPDATE 1. Does direcory /home/domainname/public_html/guest exist on server?

Add following method to your class:

private static void showServerReply(FTPClient ftpClient) {
    String[] replies = ftpClient.getReplyStrings();
    if (replies != null && replies.length > 0) {
        for (String aReply : replies) {
            System.out.println("SERVER: " + aReply);
        }
    }
}

and call it after every ftp-client's method call. This will give you codes and descriptions of every command result. I suspect client.changeWorkingDirectory(...) ends with error, probably: 550 Permission Denied (or No such file or folder).

Next modification will be:

client.login(Util.getProductsXMLFTPUser(), Util.getProductsXMLFTPPassword());
System.out.println(client.printWorkingDirectory()); // added this line!

this will tell us what is current working directory after login in.

Please post your results.

Upvotes: 2

Related Questions