【问题标题】:Java + Apache Commons Net: Read FTPFile Without Downloading [closed]Java + Apache Commons Net:无需下载即可读取 FTPFile [关闭]
【发布时间】:2013-01-23 18:46:47
【问题描述】:

使用 Apache Commons Net 3.2,我的程序连接到 FTP 服务器并从中下载文件。

然而,它应该能够做的是读取服务器上的文件而不下载它们。

这可能吗?

只是服务器包含大量个人信息,SSN、电话、邮箱等,只有拥有正确密码的特定人员才能访问。

任何人都不应该能够从服务器下载任何东西,至少在我的程序中没有授予最高权限的情况下是这样!

到目前为止,我有一个包含服务器上所有数据文件的 FTPFile []。

我想遍历它们,看看他们是否有当前用户的名字(意味着他们可以查看这个人/文件),如果有,将他们的数据添加到 ArrayList 中。

有什么建议吗?

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Arrays;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

public class Server {
    private static final String server = "/server";
    private static final String host = "00.000.0.0";
    private static final String user = "username";
    private static final String pass = "password";
    private static List <FTPFile> data;
    private static FTPClient ftp = new FTPClient ();

    public static void load (String folder) {
        try {
            // Connect to the SERVER
            ftp.connect(host, 21);
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                System.out.println("Could not connect to the server.");
                return;
            }

            // Login to the SERVER
            ftp.enterLocalPassiveMode();
            if (!ftp.login(user, pass)) {
                System.out.println("Could not login to the server.");
                return;
            }

            // Get DATA from the SERVER
            System.out.println(server + "/" + folder);
            data = Arrays.asList(ftp.listFiles(server + "/" + folder));
            System.out.println(data.size());
            for (int f = 0; f < data.size(); f++)
                System.out.println(data.get(f).getName());

            // Disconnect from the SERVER
            ftp.logout();
            ftp.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String read (FTPFile file) {
        try {
            String name = file.getName();
            File tempFolder = new File ("temp/" + name);
            tempFolder.mkdirs();

            // Create a TEMPORARY DATA FILE
            File tempFile = new File (tempFolder.getAbsolutePath() + "/data");
            System.out.println(tempFile.getAbsolutePath());
            tempFile.createNewFile();
            tempFile.deleteOnExit();

            // Get ready to DOWNLOAD DATA from the SERVER
            FileOutputStream out = new FileOutputStream (new File (name));
            ftp.connect(host, 21);
            ftp.login(user,  pass);

            // DOWNLOAD and DISCONNECT
            ftp.retrieveFile(name, out);
            out.close();
            ftp.logout();
            ftp.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ""; // This should return a String with data read from the file
    }
}

【问题讨论】:

  • 你能在电影屏幕上看电影而不用投影仪照射吗?
  • 你能在 Stack 上提问而不会受到冷嘲热讽吗?
  • @RichYoung 这个问题确实与 Java 或 Apache Commons Net 无关。这就是 FTP 的工作方式。协议中没有列出文件内容的规定。毕竟是文件传输协议:)

标签: java apache-commons-net readfile


【解决方案1】:

如果您想“读取”(如检查其内容)您必须下载的文件,即将其内容从服务器传输到客户端。当您受限于客户端解决方案时,没有办法解决。

一般来说,只在客户端检查用户权限是个坏主意。如果恶意用户拥有访问服务器的正确凭据(可以从客户端提取),他可以绕过客户端授权,使其无用。

用户的身份验证和授权应始终在服务器端进行。

在您的示例中,您可以考虑在 ftp 服务器上创建不同的用户以及访问、读取和写入文件/目录的适当权限。

【讨论】:

    【解决方案2】:

    不,如果不先下载文件,就无法通过 ftp 从客户端读取文件。

    您必须在 (ftp-) 服务器端安装软件。

    【讨论】:

      猜你喜欢
      • 2015-09-12
      • 2012-06-23
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多