无法上传文件的FTP服务器上的Android(Unable to Upload file on FT

2019-09-19 07:30发布

我想上传文件中像这样

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

下面是我的logcat的状态

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

我要上传文件的“新目录 ”文件夹和文件名aa.txt文件 **,但它给上传状态假 。**

在服务器或在我的代码的问题??? 请帮忙!!!!!!!!! 提前致谢

Answer 1:

我尝试下面的代码,我用它来上传和下载服务器上的歌曲 。 我使用的是Apache的共同库。

请为目录的变化,并在下面的代码文件名。

上传:

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






    }

下载:

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



}


文章来源: Unable to Upload file on FTP Server in android
标签: java android ftp