Android:读取远程位置文件夹名称和文件名

时间:2018-11-23 11:58:24

标签: android directory subdirectory file-access

我已连接到地址为192.168.1.254的服务器,并且在浏览器中键入此地址时,它会显示可用文件夹enter image description here的列表 我想在我的android应用程序中显示该文件夹的名称,但是我尝试了以下代码,但是没有运气。

try {
        SmbFile test = new SmbFile("smb://192.168.1.254");
        SmbFile[] files = test.listFiles();
        if (files != null)
            for (SmbFile s : files) {
                Log.d("debug", s.getName());
            }
    } catch (SmbException e) {
        Log.d("debug", "ERROR");
    } catch (Exception e) {
        Log.d("debug", "ERROR");
    }
我发现here

我也尝试过

File f = new File("//192.168.1.254");
//also tried with File f = new File("http//192.168.1.254");
File[] files = f.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
    // Specify the extentions of files to be included.
    return name.endsWith(".bmp") || name.endsWith(".gif");
}
});

 // get names of the files
String[] fileNamesArray = null; 
for (int indx = 0; indx < files.length(); indx++) {
Log.d("name",files[indx].getName());
}

return fileNamesArray; 

2 个答案:

答案 0 :(得分:0)

似乎您需要FTP客户端来获取远程位置上的列表文件。尝试使用ftp4jofficial documentation中描述的this Android Example库(通过devert)。通过Arun,用ftp4j精确列出远程FTP服务器上的文件,您可以找到here

    FTPClient client = null;
    try {       // Get the FTP Connection from the
        Utility class client =FTPUtility.connect(ipAddress, userName,
                password);
        if (client != null) {           /* List all file inside the directory */
            FTPFile[] fileArray = client.list();
            System.out.println("List of files...");
            for (int i = 0; i < fileArray.length; i++) {
                FTPFile file = fileArray[i];
                if (file != null) {
                    if (file.TYPE_FILE == FTPFile.TYPE_FILE) // File                    {
                        System.out.println("File Name = " + file.getName() + " ; File Size = " + file.getSize() + " ;Modified Date = " + file.getModifiedDate());
                } else if (file.TYPE_DIRECTORY == FTPFile.TYPE_DIRECTORY) // Directory
                {
                    System.out.println("Directory Name = " + file.getName() + " ; Directory Size = " + file.getSize() + " ;Modified Date = " + file.getModifiedDate());
                } else if (file.TYPE_LINK == FTPFile.TYPE_LINK) // Link
                {
                    System.out.println("Link Name = " + file.getName() + " ;Modified Date = "
                            + file.getModifiedDate());
                }
            }
        }
    }
}   catch(
Exception e)

{
    System.err.println("ERROR : Error in Connecting to Remote Machine... Hence exitting...");       //
    e.printStackTrace();
    System.exit(2);
}

finally

{
    try {
        client.disconnect(true);
    } catch
            (Exception e) {
    }
}

更新

如果“没有ftp的活动端口,而我目前的发现是设备只有4个活动端口,即80,443,3333,8192”,似乎文件列表通过HTTP发送,您可以通过HttpURLConnection下载,解析响应。像这样:

HttpURLConnection connection = null;
BufferedReader reader = null;

try {
    URL url = new URL("http://192.168.1.254");

    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.connect();

    int responseCode = connection.getResponseCode();

    InputStream stream = connection.getInputStream();

    reader = new BufferedReader(new InputStreamReader(stream));
    StringBuilder responseStringBuilder = new StringBuilder();

    String line = "";

    while ((line = reader.readLine()) != null) {
        responseStringBuilder .append(line);
        responseStringBuilder .append("\n");
    }

    // Parse responseStringBuilder.toString() (probably as HTML) here:
    ... 

} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (connection != null) {
        connection.disconnect();
    }
    try {
        if (reader != null) {
            reader.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

答案 1 :(得分:0)

一种方法是从服务器下载html作为字符串。然后使用

 urlConnection = new URL("your_url").openConnection();
 reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

 while ((String line = reader.readLine()) != null) {
            stringBuilder.append(line).append("\n");
        }
 String your_data = Html.fromHtml(stringBuilder.toString());

这将包含文本格式的表格。您可以对其进行处理以获取所需的数据。