我已经连接了ftp服务器,我怎么能得到这个文件路径?

时间:2014-03-23 19:14:06

标签: android ftp urlconnection

public void runftpConnect(){
    boolean status = false;
    // Replace your UID & PW here


    status = ftpclient.ftpConnect("192.168.0.109", "root", "s20041644", 21);
    File dir = /*Environment.getExternalStorageDirectory();*/
    if (status == true) {
        Log.d(TAG, "Connection Success");
        localPrintFilesList(dir, "LOCAL FILES");
        Log.d(TAG, "localPrintFilesList");
        //status = ftpclient.ftpUpload(TEMP_FILENAME, TEMP_FILENAME, "/", cntx);

    } else {
        //Toast.makeText(getApplicationContext(), "Connection failed", 2000).show();
        Log.d(TAG, "Connection failed");
    }
}

如此。虽然我可以连接ftp服务器,但我怎样才能获得文件路径?

我试过

`String mString = "ftp://192.168.0.109/";

File dir = new File(mString);`

但它没有用。有什么办法可以通过修改这个来获取ftp文件路径吗?

File dir = /*Environment.getExternalStorageDirectory();*/

1 个答案:

答案 0 :(得分:0)

替换它:

File dir = /*Environment.getExternalStorageDirectory();*/

有了这个:

FTPFile[] dirs = null;
FTPFile[] files = null;
try {
    dirs = client.listDirectories();
    files = client.listFiles();
} catch (IOException e) {
    e.printStackTrace();
}

现在,您可以在foreach中使用FTPFile来收集有关文件或目录的信息。或者您可以通过以下方式获取您所连接目录的路径:

try{
    String path = ftpclient.printWorkingDirectory();
} catch (IOException e) {
    e.printStackTRace();
}