基于APACHE COMMONS中的NET包提供的FTP客户端类库实现,下面是具体的代码:
package test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class ClientTest {
public static void main(String[] args) {
ClientTest main = new ClientTest();
FTPClient ftp = new FTPClient();
// 设置一个监听
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
try {
ftp.connect("localhost", 2121);
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.out.println("FTP server refused connection");
}
// 登录
ftp.login("admin", "admin");
main.uploadFile(ftp);
main.listFiles(ftp);
main.downloadFile(ftp);
main.deleteFile(ftp);
main.listFiles(ftp);
// 退出
ftp.logout();
} catch (IOException e) {
e.printStackTrace();
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (Exception e2) {
}
}
}
}
// 列举文件
private void listFiles(FTPClient ftp) throws IOException {
System.out.println("------------START------------");
for (FTPFile f : ftp.listFiles()) {
System.out.println(f.getName() + " " + f.getSize());
}
System.out.println("------------END------------");
}
// 上传文件
private void uploadFile(FTPClient ftp) throws IOException {
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
InputStream input = new FileInputStream("res/conf/dushu.jpg");
ftp.storeFile("dushu.jpg", input);
input.close();
}
// 下载文件
private void downloadFile(FTPClient ftp) throws IOException {
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
OutputStream output = new FileOutputStream("res/conf/dushu_down.jpg");
ftp.retrieveFile("dushu.jpg", output);
output.close();
}
// 删除文件
private void deleteFile(FTPClient ftp) throws IOException {
ftp.deleteFile("dushu.jpg");
}
}
输出如下:
220 Service ready for new user.
USER admin
331 User name okay, need password for admin.
PASS admin
230 User logged in, proceed.
TYPE I
200 Command TYPE okay.
PASV
227 Entering Passive Mode (127,0,0,1,13,12)
STOR dushu.jpg
150 File status okay; about to open data connection.
226 Transfer complete.
------------START------------
SYST
215 UNIX Type: Apache FtpServer
PASV
227 Entering Passive Mode (127,0,0,1,13,14)
LIST
150 File status okay; about to open data connection.
226 Closing data connection.
dushu.jpg 83694
------------END------------
TYPE I
200 Command TYPE okay.
PASV
227 Entering Passive Mode (127,0,0,1,13,16)
RETR dushu.jpg
150 File status okay; about to open data connection.
226 Transfer complete.
DELE dushu.jpg
250 Requested file action okay, deleted /dushu.jpg.
------------START------------
PASV
227 Entering Passive Mode (127,0,0,1,13,18)
LIST
150 File status okay; about to open data connection.
226 Closing data connection.
------------END------------
QUIT
221 Goodbye.
最后,提供一张自己拍的读书图: