需要com.springsource.org.apache.commons.net-1.4.1.jar和com.springsource.org.apache.oro-2.0.8.jar两个FTPClient的包
public static void main(String[] args) {
//上传文件
FTPClient ftp = new FTPClient();
try {
int reply;
//url FTP服务器hostname ,port FTP服务器端口
ftp.connect("172.0.0.1", 21);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
//创建输入流
FileInputStream input = new FileInputStream("D:\\apache-tomcat-6.0.18\\webapps\\FTDMProject\\upload\\20130703\\test.txt");
//username FTP登录账号、密码
ftp.login("sa", "sa");//登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
//FTP服务器保存目录
ftp.changeWorkingDirectory("/ProjectMaterial/file/2013/7/3");
//上传到FTP服务器上的文件名,输入流
ftp.storeFile("test", input);
input.close();
ftp.logout();
System.out.println("ok");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
}
public static void main(String[] args) {
//下载文件
try{
int port = 21;
FTPClient ftpClient = new FTPClient();
ftpClient.connect(ftpServer, port);
ftpClient.login(loginName, password);
//FTP服务器保存目录
ftpClient.changeWorkingDirectory(filePath);
/创建/输出流
FileOutputStream fos = new FileOutputStream(downfile+fileName);
//文件名,输出流
ftpClient.retrieveFile(fileName, fos);
fos.flush();
fos.close();
}catch(IOException e){
e.printStackTrace();
}
}