简体   繁体   中英

Java Android Upload image to server FTPClient - Error 501 PASV: Operation not permitted

When trying to upload the image to the server

clientFtp.getReplyCode()

returns the value of 501 and

clientFtp.getReplyString()

returns 501 PASV: Operation not permitted , it reaches the validation of

FTPReply.isPositiveCompletion

and it returns false for which does not let me move forward, it disconnects the clientFtp and the image does not upload. It then continues to close the input and when it reaches

clientFtp.logout()

This throws me into the catch and prints the following error in the Log java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.BufferedWriter.write(java.lang.String)' on a null object reference The strange thing is that I try it with several devices and 99% if it works and 1% throws me these errors, my code is here;

public static class uploadFileFtp extends AsyncTask<String, Integer, Boolean> {

        String absolutePath;
        String imagename;
        String binder;
        String idContrato;

        public uploadFileFtp(String absolutePath, String imagename, String binder, String idContrato) {
            this.absolutePath = absolutePath;
            this.imagename = imagename;
            this.binder = binder;
            this.idContrato = idContrato;
        }

        @Override
        protected Boolean doInBackground(String... strings) {

            FTPClient clientFtp = null;
            try{

                clientFtp = new FTPClient();

                clientFtp.connect(keys.hostname,keys.port);
                clientFtp.setSoTimeout(10000);
                clientFtp.enterLocalPassiveMode();
                if(clientFtp.login(keys.username,keys.password)){
                    clientFtp.setFileType(FTP.BINARY_FILE_TYPE);
                    clientFtp.setFileTransferMode(FTP.BINARY_FILE_TYPE);

                    InputStream input = new FileInputStream(absolutePath);
                    clientFtp.storeFile("/" + keys.ftp_home_folder + "/" + binder + "/" + imagename, input);
                    int reply = clientFtp.getReplyCode();
                    String replyString = clientFtp.getReplyString();
                    if(!FTPReply.isPositiveCompletion(reply)){
                        clientFtp.disconnect();
                        Log.i("MESSAGE","THE SERVER DID NOT ALLOW TO CONNECT");
                    }
                    input.close();
                    clientFtp.logout();
                    return true;
                }
                return false;

            }catch(Exception e){
                global.writeError(e, 97);
                Log.i("MESSAGE","Error: "+e);
                return false;
            }finally {
                if(clientFtp.isConnected()){
                    try{clientFtp.disconnect();}
                    catch(Exception e){
                        global.writeError(e, 98);
                        Log.i("MESSAGE","Error: "+e);
                    }
                }
            }
        }

        @Override
        protected void onPostExecute(Boolean send) {
            if (send) {
                Log.i("MESSAGE", "Send");
            }else {
                Log.i("MESSAGE", "Not sent");
            }
        }
    }

I hope you can help me, thanks

You call logout() after disconnect().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM