0
votes

I have read a half dozen threads regarding this and I'm no where closer to a solution. No matter what I change I get ftp return code 500 "The command was not accepted and the requested action did not take place." and I'm not sure how to debug that.

This is my site and I can connect with CoreFTP and read and write, so it does not seem to be a permissions issue. I've tried two different accounts, with this code and CoreFTP. One writes to the root and another is pointed to an "image_in" folder.

imageData is a byte array with a length of 166578 and stream has the same length after the InputStream call. storeFile() always returns false with a return code of 500.

One thread implied enterLocalPassiveMode() and enterRemotePassiveMode() were the culprits, but I have tried this code both with and without those lines and still I get a 500 return code.

Any idea what I'm missing?

Greg

class ImageUploadTask extends AsyncTask <Void, Void, String>{
    @Override
    protected String doInBackground(Void... unsued) {
        try {
            boolean status = false;
            try {
                FTPClient mFtpClient = new FTPClient();
                String ip = "my domain dot com";
                String userName = "ftp79815757-0";
                String pass = "my password";

                mFtpClient.connect(InetAddress.getByName(ip));
                mFtpClient.login(userName, pass);
                int reply = mFtpClient.getReplyCode();
                if (FTPReply.isPositiveCompletion(reply)) {

        //one thread said this would do the trick
                    mFtpClient.enterLocalPassiveMode();
                    mFtpClient.enterRemotePassiveMode();

                    InputStream stream = new ByteArrayInputStream(imageData);

                    mFtpClient.changeWorkingDirectory("/images_in");                        
                    String currdir = mFtpClient.printWorkingDirectory();

                    if (!mFtpClient.storeFile("remoteName.jpg", stream)) {
                        Log.e("FTPUpload", String.valueOf(mFtpClient.getReplyCode()));
                    }

                    stream.close(); 
                    mFtpClient.disconnect();
                }
                else {
                    Log.e("FTPConnected", String.valueOf(reply));
                }
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        } catch (Exception e) {
            if (dialog.isShowing())
                dialog.dismiss();
            Toast.makeText(getApplicationContext(),
                    "Error",
                    Toast.LENGTH_LONG).show();
            Log.e(e.getClass().getName(), e.getMessage(), e);
            return null;
        }
    }
2
Do you get any other exceptions? And have you added the required permissions in your Manifest.xml ? - Sharp Edge
No Java exceptions in the code and no other FTP exceptions. The permissions in the manifest are Internet, write external storage, and read contacts. Internet was added when I incorporated FTP. Are other manifest permissions needed? - user1091524
I have also tried to explicitly set the port to 21 and I use both mFtpClient.setFileType(FTP.ASCII_FILE_TYPE) and mFtpClient.setFileType(FTP.BINARY_FILE_TYPE); Still get return code 500, - user1091524
So you want to upload files from android using FTP .. which example are you following on the internet ? If you're using ftp4j then there are many working examples - Sharp Edge
Yes, ftp from android. All of the examples I've seen use org.apache.commons.net.ftp.FTP - user1091524

2 Answers

0
votes

You forgot to set file type

mFtpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);

If it still doesn't work then you have following options:

If you insist on using Apache FTP Client then follow this example

Or you could try this example

The second example worked in my case.

0
votes

You need to use: enterLocalPassiveMode

mFtpClient.enterLocalPassiveMode();

If you then do other operations, you might have to go back active with enterLocalActiveMode.