Unable to Upload file on FTP Server in android

2019-06-11 08:12发布

I am trying to Upload file like this

try {
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString
    FTPClient ftpClient = new FTPClient();
    ftpClient.connect("xxx.xxx.xx.xx");
    if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))
    {
      boolean status=ftpClient.login("username", "password");
      Log.d(TAG, "login status=="+status);
      status=ftpClient.changeWorkingDirectory("New directory");
      Log.d(TAG, "changeWorkingDirectory status=="+status);
      status=ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
      Log.d(TAG, "setFileType status=="+status);
      ftpClient.enterLocalPassiveMode();
      String srcFilePath=extStorageDirectory + "/AA.txt";
     FileInputStream srcFileStream = new FileInputStream(new File(srcFilePath));
     status=ftpClient.storeFile("AA.txt", srcFileStream);
     Log.d(TAG, "upload status=="+status);  
      ftpClient.logout();
    ftpClient.disconnect();
            }
            else
            {
                Log.d(TAG, "connectfail");
            }

        } catch (SocketException e) {
            Log.d(TAG, "SocketException status=="+e.toString());
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "FileNotFoundException status=="+e.toString());
            e.printStackTrace();
        } catch (IOException e) {
            Log.d(TAG, "IOException status=="+e.toString());
            e.printStackTrace();
        }

below is my logcat status

07-11 12:24:43.359: D/FTPDownloadDroid(10647): <!>com.ss.dr 138<!> login status==true
07-11 12:24:48.379: D/FTPDownloadDroid(10647): <!>com.ss.dr 141<!> changeWorkingDirectory status==true
07-11 12:24:48.859: D/FTPDownloadDroid(10647): <!>com.ss.dr 143<!> setFileType status==tr
07-11 12:24:54.359: D/FTPDownloadDroid(10647): <!>com.ss.dr 150<!> upload status==false

I want to Upload file on "New Directory" folder and file name AA.txt ** but it is giving Upload status false.**

Is the problem in server or in my Code??? Please help!!!!!!!!! Thanks in advance

标签: java android ftp
1条回答
女痞
2楼-- · 2019-06-11 08:26

Try my code below, i used this to upload and download a song on the server. I am using the Apache's common lib.

Please make the changes for the directories and file name in the below code.

UPLOAD:

public void goforIt(){


        FTPClient con = null;

        try
        {
            con = new FTPClient();
            con.connect("192.168.2.57");

            if (con.login("Administrator", "KUjWbk"))
            {
                con.enterLocalPassiveMode(); // important!
                con.setFileType(FTP.BINARY_FILE_TYPE);
                String data = "/sdcard/vivekm4a.m4a";

                FileInputStream in = new FileInputStream(new File(data));
                boolean result = con.storeFile("/vivekm4a.m4a", in);
                in.close();
                if (result) Log.v("upload result", "succeeded");
                con.logout();
                con.disconnect();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }






    }

DOWNLOAD:

public void goforIt(){
    FTPClient con = null;

    try
    {
        con = new FTPClient();
        con.connect("192.168.2.57");

        if (con.login("Administrator", "KUjWbk"))
        {
            con.enterLocalPassiveMode(); // important!
            con.setFileType(FTP.BINARY_FILE_TYPE);
            String data = "/sdcard/vivekm4a.m4a";

            OutputStream out = new FileOutputStream(new File(data));
            boolean result = con.retrieveFile("vivekm4a.m4a", out);
            out.close();
            if (result) Log.v("download result", "succeeded");
            con.logout();
            con.disconnect();
        }
    }
    catch (Exception e)
    {
        Log.v("download result","failed");
        e.printStackTrace();
    }



}
查看更多
登录 后发表回答