packageFTPDemo;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStream;importjava.util.logging.Logger;importorg.apache.commons.net.ftp.FTPClient;importorg.apache.commons.net.ftp.FTPFile;importorg.apache.commons.net.ftp.FTPReply;public classFtpUtil {private staticFTPClient ftp;/*** 获取ftp连接
*@paramf
*@return*@throwsException*/
public static boolean connectFtp(FtpConfig f) throwsException{
ftp=newFTPClient();boolean flag=false;if (f.getFtpPort()==null) {
ftp.connect(f.getFtpHost(),21);
}else{
ftp.connect(f.getFtpHost(),f.getFtpPort());
}
ftp.login(f.getFtpUser(), f.getFtpPassword());int reply =ftp.getReplyCode();if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();returnflag;
}
ftp.changeWorkingDirectory(f.getFtpPath());
flag= true;returnflag;
}/*** 关闭ftp连接*/
public static voidcloseFtp(){try{if (ftp!=null &&ftp.isConnected()) {
ftp.logout();
ftp.disconnect();
}
}catch(IOException e){
e.printStackTrace();
}
}/*** ftp上传文件
*@paramf
*@throwsException*/
public static void upload(File f) throwsException{if(f.isDirectory()) {
ftp.makeDirectory(f.getName());
ftp.changeWorkingDirectory(f.getName());
String[] files=f.list();for(String fstr : files){
File file1=new File(f.getPath()+File.separator+fstr);if(file1.isDirectory()) {
upload(file1);
ftp.changeToParentDirectory();
}else{
File file2=new File(f.getPath()+File.separator+fstr);
FileInputStream input=newFileInputStream(file2);
ftp.storeFile(file2.getName(),input);
input.close();
}
}
}else{
File file2=newFile(f.getPath());
FileInputStream input=newFileInputStream(file2);
ftp.storeFile(file2.getName(),input);
input.close();
}
}/*** 下载链接配置
*@paramf
*@paramlocalBaseDir 本地目录
*@paramremoteBaseDir 远程目录
*@throwsException*/
public static void startDownDir(FtpConfig f,String localBaseDir,String remoteBaseDir) throwsException{if(FtpUtil.connectFtp(f)) {try{
FTPFile[] files= null;boolean changedir =ftp.changeWorkingDirectory(remoteBaseDir);if(changedir) {
ftp.setControlEncoding("UTF-8");
files=ftp.listFiles();for (int i = 0; i < files.length; i++) {
downloadFile(files[i], localBaseDir, remoteBaseDir);
}
}else{
System.out.println("不存在的相对路径!");
}
}catch(Exception e) {
e.printStackTrace();
}
}else{
System.out.println("连接失败");
}
}public static void startDownFile(FtpConfig f,String localBaseDir,String remoteFilePath) throwsException{if(FtpUtil.connectFtp(f)) {try{
FileOutputStream outputStream= new FileOutputStream(localBaseDir +remoteFilePath);
ftp.retrieveFile(remoteFilePath, outputStream);
outputStream.flush();
outputStream.close();
}catch(Exception e) {
e.printStackTrace();
}
}else{
System.out.println("连接FTP服务器失败");
}
}/***
* 下载FTP文件
* 当你需要下载FTP文件的时候,调用此方法
* 根据获取的文件名,本地地址,远程地址进行下载
*
*@paramftpFile
*@paramrelativeLocalPath 下载到本地的绝对路径
*@paramrelativeRemotePath 要下载的远程ftp服务器相对路径*/
private static voiddownloadFile(FTPFile ftpFile, String relativeLocalPath,String relativeRemotePath) {if(ftpFile.isFile()) {if (ftpFile.getName().indexOf("?") == -1) {
OutputStream outputStream= null;try{
File locaFile= new File(relativeLocalPath+ftpFile.getName());//判断文件是否存在,存在则返回 or 直接覆盖
if(locaFile.exists()){return;
}else{
outputStream= new FileOutputStream(relativeLocalPath+ftpFile.getName());
ftp.retrieveFile(ftpFile.getName(), outputStream);
outputStream.flush();
}
}catch(Exception e) {
e.printStackTrace();
}finally{try{if (outputStream != null){
outputStream.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
}else{
String newlocalRelatePath= relativeLocalPath +ftpFile.getName();
String newRemote= relativeRemotePath +ftpFile.getName().toString();
File fl= newFile(newlocalRelatePath);if (!fl.exists()) {
fl.mkdirs();
}try{
newlocalRelatePath= newlocalRelatePath+File.separator;
newRemote= newRemote+File.separator;
String currentWorkDir=ftpFile.getName().toString();//System.out.println(currentWorkDir);
boolean changedir =ftp.changeWorkingDirectory(currentWorkDir);if(changedir) {
FTPFile[] files= null;
files=ftp.listFiles();for (int i = 0; i < files.length; i++) {
downloadFile(files[i], newlocalRelatePath, newRemote);
}
}if(changedir){
ftp.changeToParentDirectory();
}
}catch(Exception e) {
e.printStackTrace();
}
}
}public static void main(String[] args) throwsException{
FtpConfig f=newFtpConfig();
f.setFtpHost("172.25.69.14");
f.setFtpPort(21);
f.setFtpUser("anyone");
f.setFtpPassword("");//f.setFtpPath("/data1/");//相对路径
FtpUtil.connectFtp(f);
File file= new File("E:\\data1\\physics.txt");//FtpUtil.upload(file);//把文件上传在ftp上//FtpUtil.startDownFile(f, "E:/", "physics.txt");
FtpUtil.startDownDir(f, "E:/data1/", "/data1/");
}
}