gpt4 book ai didi

Java + Apache Commons Net : Read FTPFile Without Downloading

转载 作者:行者123 更新时间:2023-12-01 07:34:07 26 4
gpt4 key购买 nike

使用 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
}
}

最佳答案

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

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

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

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

关于Java + Apache Commons Net : Read FTPFile Without Downloading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14487075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com