这里记录一下java操作FTP的功能,包括上传/下载文件,文件夹,并发上传下载等,最后会分析Common-Net.jar包的内容。
先看一下常规的上传/下载文件操作的代码。
package com.lenovo.plm.dms.ftp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class Test {
public static void upload(String host,int port,String username,String password,String pathname,String remote,InputStream local) throws SocketException, IOException{
FTPClient ftp = new FTPClient();
ftp.connect(host, port);
ftp.login(username, password);
if(!FTPReply.isPositiveCompletion(ftp.getReplyCode())){
ftp.disconnect();
return;
}
ftp.changeWorkingDirectory(pathname);
ftp.storeFile(remote, local);
local.close();
ftp.logout();
}
public static void download(String host,int port,String username,String password,String pathname,String fileName,String localPath) throws SocketException, IOException{
FTPClient ftp = new FTPClient();
ftp.connect(host, port);
ftp.login(username, password);
if(!FTPReply.isPositiveCompletion(ftp.getReplyCode())){
ftp.disconnect();
return;
}
ftp.changeWorkingDirectory(pathname);
FTPFile[] fs = ftp.listFiles();
for(FTPFile file:fs){
if(file.getType() == 0){
if(file.getName().equals(fileName)){
File localFile = new File(localPath + "/" + file.getName());
OutputStream os = new FileOutputStream(localFile);
ftp.retrieveFile(file.getName(), os);
os.close();
return;
}
}
}
ftp.logout();
}
public static void downloadDir(String host,int port,String username,String password,String pathname,String localPath) throws SocketException, IOException{
FTPClient ftp = new FTPClient();
ftp.connect(host, port);
ftp.login(username, password);
if(!FTPReply.isPositiveCompletion(ftp.getReplyCode())){
ftp.disconnect();
return;
}
ftp.changeWorkingDirectory(pathname);
FTPFile[] fs = ftp.listFiles();
for(FTPFile file:fs){
if(file.getType() == 1){
downloadDir(host,port,username,password,pathname+"/"+file.getName(),localPath);
}else{
File temp = new File(localPath+pathname);
if(!temp.exists()){
temp.mkdirs();
}
File localFile = new File(localPath + pathname + "/" + file.getName());
OutputStream os = new FileOutputStream(localFile);
ftp.retrieveFile(file.getName(), os);
os.close();
}
}
ftp.logout();
}
public static void main(String[] args) throws FileNotFoundException {
// upload file
InputStream local = new FileInputStream(new File("D:/software/gwt-2.7.0.zip"));
try {
upload("10.120.24.111",21,"admin","admin","/uploaded","gwt-2.7.0.zip",local);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// download file.
try {
download("10.120.24.111",21,"admin","admin","/uploaded/","RDD.txt","d:/");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
downloadDir("10.120.24.111",21,"admin","admin","/lenovo","d:/temp");
}
}
单纯的上传,下载文件,下载文件夹比较简单,因此做个记录。下一次学习并发下载文件夹。