使用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
}
}
答案 0 :(得分:4)
如果您想要“读取”(如检查其内容)您必须下载的文件,即将其内容从服务器传输到客户端。当你被限制在客户端解决方案时,没有办法解决它。
通常,仅检查客户端中的用户权限是一个坏主意。如果恶意用户具有访问服务器的正确凭据(可以从客户端提取),他可以规避客户端授权,使其无效。
用户的身份验证和授权应始终在服务器端进行。
在您的示例中,您可以考虑在ftp服务器上创建不同的用户以及访问,读取和写入文件/目录的适当权限。
答案 1 :(得分:3)
如果没有先下载它,不能通过ftp从客户端读取文件。
您必须在(ftp-)服务器端安装软件。