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