使用FTP over SSL Android上传文件时出现错误

时间:2016-03-07 07:18:55

标签: android apache ssl ftp ftps

我正在使用Apache commonsnetFTP。我可以在FTP服务器中创建文件夹。但每当我storeFile失败并且给我550错误时。

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;
}

它正在连接,验证,创建新文件夹,但storingFile / getReplyCode始终返回550.这里有什么问题?我也删除了KeyManagerFactory但没有运气。

0 个答案:

没有答案