ftp服务器的文件夹递归上传与下载


import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;
import java.io.FileOutputStream;  
  




import java.io.IOException;
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;  


import com.zealot.Zealot;


import common.Logger;
  
  
/** 
 *  
 * 文件上传下载到FTP服务器 
 *  
 */  
public class FtpUtil {  
public static Logger logger = Logger.getLogger(FtpUtil.class); 
    private static FTPClient ftp;  
    private static String localPath="D:\\ftpdownload";  
    private static String  address = Zealot.FTPADRESS;  
    private static int port = Integer.parseInt(Zealot.FTPPORT);  
    private static String username = Zealot.FTPUSERNAME;  
    private static String password = Zealot.FTPPASSWORD;  
    private static int reply; 
    static {  
         
        /*try {  
            ftp.connect(address, port);  
            ftp.login(username, password);
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);  
            reply = ftp.getReplyCode();  
            if (!FTPReply.isPositiveCompletion(reply)) {  
                ftp.disconnect();  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  */
    }  
    public static void conFtp(){
    ftp = new FTPClient();
    try {
ftp.connect(address, port);  
ftp.login(username, password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);  
reply = ftp.getReplyCode();  
if (!FTPReply.isPositiveCompletion(reply)) {  
    ftp.disconnect();  
    logger.info("------ftp服务器连接成功!------");
}
} catch (SocketException e) {
logger.info("------ftp服务器连接失败!------");
e.printStackTrace();
} catch (IOException e) {
logger.info("------ftp服务器连接失败!------");
e.printStackTrace();
}  
    }
    
    /** 
     *  
     * @param file 
     *            上传的文件或文件夹 
     * @throws Exception 
     */  
    public static void upload(File file){  
  
        try {
if (file.isDirectory()) {  
    ftp.makeDirectory(file.getName());  
    ftp.changeWorkingDirectory(file.getName());  
    String[] files = file.list();  
    for (int i = 0; i < files.length; i++) {  
        File file1 = new File(file.getPath() + File.separator + files[i]);  
        if (file1.isDirectory()) {  
            upload(file1);  
            ftp.changeToParentDirectory();  
        } else {  
            File file2 = new File(file.getPath() + File.separator + files[i]);  
            FileInputStream input = new FileInputStream(file2);  
            ftp.storeFile(file2.getName(), input);  
            input.close();  
        }  
    }  
} else {  
    File file2 = new File(file.getPath());  
    FileInputStream input = new FileInputStream(file2);  
    ftp.storeFile(file2.getName(), input);  
    input.close();  
}
logger.info("**************************led显示屏图片上传ftp成功**************************");
} catch (FileNotFoundException e) {
logger.info("**************************led显示屏图片上传ftp失败**************************");
e.printStackTrace();
} catch (Exception e) {
logger.info("**************************led显示屏图片上传ftp失败**************************");
e.printStackTrace();
}  
    }  
  
    /** 
     *  
     * @param path 需要下载的文件或文件夹路径(如:\\test 表示ftp根目录的test文件夹) 
     * @throws Exception 
     */  
    public static void download(String path) throws Exception {  
        FileOutputStream fos =null;  
        File dir = new File(localPath+File.separator+path);  
        if (!dir.exists() && !dir.isDirectory()) {  
            dir.mkdirs();  
        }  
        FTPFile[] files = ftp.listFiles(path);  
        for (int i = 0; i < files.length; i++) {  
            if (files[i].isDirectory()) {  
                File file1 = new File(localPath+File.separator +path+File.separator+ files[i].getName());  
                if (!file1.exists() && !file1.isDirectory()) {  
                    file1.mkdirs();  
                }  
                download(path+File.separator + files[i].getName());  
            } else {  
                System.out.println(localPath+File.separator+path+File.separator+files[i].getName());  
                File file2 = new File(localPath+File.separator+path+File.separator+files[i].getName());  
                fos = new FileOutputStream(file2);  
                ftp.retrieveFile(path+File.separator+files[i].getName(), fos);  
                fos.close();  
            }  
        }  
          
    }  
  
    public static void closeCon(){  
        if(ftp !=null){  
            if(ftp.isConnected()){  
                try {  
                    ftp.logout();  
                    ftp.disconnect();  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }   
            }  
        }  
    }
    public static void deleteFile(String ftpPath) throws Exception{
    FTPFile[] files = ftp.listFiles(ftpPath); 
    for(FTPFile f:files){ 
    String path = ftpPath+File.separator+f.getName();
    if(f.isDirectory()){
    deleteFile(path);
    }else if(f.isFile()){
    ftp.deleteFile(path); 
    }
    }
    ftp.removeDirectory(ftpPath);
    }
    public static void main(String[] args) throws Exception { 
    File file=new File("D://led//image");
    FtpUtil.conFtp();
    FtpUtil.deleteFile(file.getName());
    System.out.println(file.getName());
    upload(file);  
    }  
  
}  

你可能感兴趣的:(java)