1
votes

I know there are many similar questions asked before. I looked all of them and tried all possible solutions including changed several FTP servers, but I still could not solve my problem. There is no problem with FTP command line though. I also turned off firewall. Here is my code snippet:

        ftpClient.login(username, password);

        int mode = ftpClient.getDataConnectionMode();
        if(mode == ftpClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE)
            ftpClient.enterLocalPassiveMode();
        else if(mode == ftpClient.ACTIVE_LOCAL_DATA_CONNECTION_MODE)
            ftpClient.enterLocalActiveMode();

        ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
        ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
        //ftpClient.setPassiveNatWorkaround(false);

        if(LOGD) Log.d("before create out");
        String out = orderToString();
        if(LOGD) Log.d("out="+out);
        InputStream stream = new ByteArrayInputStream(out.getBytes("UTF-8"));

        if(LOGD) Log.d("before upload orders.xml");
        boolean res = ftpClient.storeFile("orders.xml", stream);
        if(!res) Log.d("store file failed");
        ftpClient.completePendingCommand();
        stream.close();

Here is the logcat:

........................................

D/SalesManager( 408): before create out D/SalesManager( 408): out= D/SalesManager( 408): D/SalesManager( 408):
1 D/SalesManager( 408): 2013-10-06 12:07:39 D/SalesManager( 408): 1 D/SalesManager( 408): 617.25 D/SalesManager( 408): D/SalesManager( 408): D/SalesManager( 408): before upload orders.xml D/SntpClient( 60): request time failed: java.net.SocketException: Address family not supported by protocol W/System.err( 408): org.apache.commons.net.ftp.FTPConnectionClosedException: FTP response 421 received. Server closed connection. D/SntpClient( 60): request time failed: java.net.SocketException: Address family not supported by protocol MainActivity$LoadAsyncTask.doInBackground(MainActivity.java:1) W/System.err( 408): at android.os.AsyncTask$2.call(AsyncTask.java:185) W/System.err( 408):
at java.util.concurrent.FutureTask$Sync. innerRun(FutureTask.java:305) W/System.err( 408): at java.util.concurrent.FutureTask.run(FutureTask.java: 137) W/System.err( 408): at java.util.concurrent.ThreadPoolExecutor. runWorker(ThreadPoolExecutor.java:1068) W/System.err( 408): at java.util.concurrent.ThreadPoolExecutor$Worker. run(ThreadPoolExecutor.java:561) W/System.err( 408): at java.lang.Thread.run(Thread.java:1096)

.......................

EDIT I ever made download a text file successfully but never make uploading a text file through. At one time during testing I ever made uploading a text file half done -- an empty same-name file is created on the server.

2

2 Answers

0
votes

use ftp4j solved the problem. several days wasted on commons.ftpclient

0
votes

I succeeded upload file to FTP server by removing completePendingCommand(). Just storeFile + getReplyCode for checking response code.

ftpClient.setDefaultTimeout(1000*60);
ftpClient.connect(server, port);
ftpClient.login(user, pass);

int replyCode=ftpClient.getReplyCode();
//log or check response

ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

InputStream inputStream=new FileInputStream(zipFile);
ftpClient.storeFile(zipFile.getName(), inputStream);
inputStream.close();

replyCode=ftpClient.getReplyCode();
//log or check response