【问题标题】:Android : Read Remote Location Folder Name and file NameAndroid:读取远程位置文件夹名称和文件名
【发布时间】:2019-04-26 01:02:38
【问题描述】:

我已连接到地址为 192.168.1.254 的服务器,在浏览器中输入此地址时,它会显示可用文件夹列表 我想在我的 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; 

【问题讨论】:

    标签: android directory subdirectory file-access


    【解决方案1】:

    一种方法是从服务器下载 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());
    

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

    【讨论】:

      【解决方案2】:

      看来您需要 FTP 客户端来获取远程位置的列表文件。例如,尝试使用 ftp4j 库,如 devert 的 official documentationthis Android Example 中所述。通过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();
          }
      }
      

      【讨论】:

      • 我之前尝试过(不是以编程方式,而是使用 filezilla),但发现没有用于 ftp 的活动端口,我目前的发现是设备只有 4 个活动端口,即 80,443,3333,8192
      • @ShubhamJain 请查看有关HttpURLConnection的更新。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 1970-01-01
      • 2013-08-08
      • 2016-03-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-03
      相关资源
      最近更新 更多