ここからFTPサーバーからディレクトリをリストするためのチュートリアルを使用していますjava.net.SocketException: Software caused connection abort: socket write error
。しかし、画像も添付されているため、例外が発生してい
ます。ftp4jライブラリとサンプルコードを使用しましたが、エラーが発生しました。何も表示されないので、理由を教えてください。この背後にある理由は、JVM のセキュリティ制限を有効にする必要があるのでしょうか? (ただの考え) おとなしくありがとう
1941 次
1 に答える
0
のような任意の FTP クライアントを使用してこの FTP サーバーに接続しようとした場合FileZilla
、Apache Commons FTP に基づいてファイルとディレクトリを一覧表示するコードを次に示します (SERVER_NAME、USER_NAME、および PASSWORD を定義します)。
FTPClient ftp = new FTPClient();
DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
FTPClientConfig config = new FTPClientConfig();
//optional - set timezone of the server
config.setServerTimeZoneId("America/New_York");
ftp.configure(config );
try {
int reply;
ftp.connect(SERVER_NAME);
ftp.login(USER_NAME, PASSWORD);
System.out.println("Connected to " + SERVER_NAME + ".");
System.out.println(ftp.getReplyString());
//After connection attempt, check the reply code to verify success.
reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.out.println("Failed to connect to FTP server");
System.exit(1);
}
//use binary mode
ftp.setFileType(FTP.BINARY_FILE_TYPE);
//use passive mode for firewalls
ftp.enterLocalPassiveMode();
FTPListParseEngine engine = ftp.initiateListParsing(".");
while (engine.hasNext()) {
FTPFile[] files = engine.getNext(25);
for (FTPFile file : files) {
String details = file.getName();
if (file.isDirectory()) {
details = "[" + details + "]";
}
details += "\t\t" + file.getSize();
details += "\t\t" + dateFormater.format(file.getTimestamp().getTime());
System.out.println(details);
}
}
ftp.logout();
} catch(IOException e) {
e.printStackTrace();
} finally {
if(ftp.isConnected()) {
try {
ftp.disconnect();
} catch(IOException ioe) {
// do nothing
}
}
}
チュートリアルで扱われていないいくつかのポイント:
- ファイアウォールに対処するためのパッシブ モード
- FTP ファイルのページネーション
- 返信コードの確認
于 2013-07-23T08:49:35.447 に答える