0

我正在尝试像这样上传文件

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上上传文件** 但它给出的上传状态为假。**

问题出在服务器还是在我的代码中???请帮忙!!!!!!!!!提前致谢

4

1 回答 1

2

试试下面的代码,我用它在服务器上上传和下载一首歌曲。我正在使用 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();
    }



}
于 2012-07-11T07:38:20.553 回答