550 Error on Uploading Files using FTP over SSL An

2019-08-07 14:31发布

I'm using Apache commonsnet library for FTP. I'm able to create the folders in FTP server. But whenever I storeFile it's failing and returning me 550 error.

FTPUpload

private boolean ftpUpload(String remoteFilePath) {
    FTPSClient ftpsClient = null;
    try {
        ftpsClient = new FTPSClient("SSL", true);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    FileInputStream fis;
    boolean result = false;
    try {
        if (ftpsClient != null) {
            ftpsClient.setConnectTimeout(10000);

            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(null, null);
            KeyManager km = kmf.getKeyManagers()[0];
            ftpsClient.setKeyManager(km);

            ftpsClient.connect(InetAddress.getByName(getString(R.string.ftp_hostname)), 990);
            boolean isLoggedIn = ftpsClient.login(getString(R.string.ftp_username), getString(R.string.ftp_password));

            if (isLoggedIn) {
                // On Successful login
                ftpsClient.setFileType(FTP.BINARY_FILE_TYPE);
                ftpsClient.enterLocalPassiveMode();
                ftpsClient.setBufferSize(1000);

                // Check remotePath exists else create folders
                int index = remoteFilePath.lastIndexOf("/");
                String remoteDir = null;
                if (index != -1)
                    remoteDir = remoteFilePath.substring(0, index);

                if (remoteDir != null)
                /**
                 * changeWorkingDirectory returns whether the specified directory exist or not.
                 * If not exist in remote, create folders
                 */
                    if (!ftpsClient.changeWorkingDirectory(remoteDir)) {
                        if (makeDirectories(ftpsClient, remoteDir))
                            ftpsClient.changeWorkingDirectory(remoteFilePath);
                        else
                            Log.e(this.getClass().getSimpleName(), "remote path is not available");
                    } else
                        ftpsClient.changeWorkingDirectory(remoteFilePath);

                //Testing
                File localFilePath = new File(Environment.getExternalStorageDirectory() + "/qwerty.png");

                ftpsClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

                try{
                    int reply = ftpsClient.getReplyCode();
                    if (!FTPReply.isPositiveCompletion(reply)) {
                        ftpsClient.disconnect();
                        Log.e(this.getClass().getSimpleName(), "Connection Refused by server");
                        return false;
                    }
                } catch(Exception e) {
                    e.printStackTrace();
                }

                // Get the stream of the file
                String testName = localFilePath.getName();
                fis = new FileInputStream(localFilePath);

                // Upload file to the ftp server
                result = ftpsClient.storeFile(testName, fis);
                fis.close();

                Log.i(this.getClass().getSimpleName(), "FTP Store Result " + result);
                if (result) {
                    result = true;
                } else {
                    result = false;
                }
            } else {
                Log.e(this.getClass().getSimpleName(), "Login Fail!");
                result = false;
            }

            // logout from ftp server
            ftpsClient.logout();

        }
    } catch (Exception e) {
        Log.e(this.getClass().getSimpleName(), "FTP Exception!");
        result = false;
    }

    return result;
}

It's connecting, authenticating, created new folder but storingFile/getReplyCode always returns 550. What is wrong here? I have removed KeyManagerFactory also but no luck.

0条回答
登录 后发表回答