FTP上传下载删除

Java FTP客户端操作

导入包:commons-net-xx.jar



import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;


public class FtpUtil {

    private static String CONTROL_ENCODING = "GBK";
    private static int DATE_TIMEOUT = 60000;//设置传输超时时间为60秒 
    private static int CONNECT_TIMEOUT = 460000;//设置链接超时时间为60秒 
    private static FTPClient ftp;

    //登陆服务
     public static void loginFTPService(String host, int port, String username, String password){
         ftp = new FTPClient();
        try {
            int reply = 0; 
            ftp.setDataTimeout(DATE_TIMEOUT);       
            ftp.setConnectTimeout(CONNECT_TIMEOUT);     
            ftp.connect(host, port);
            reply = ftp.getReplyCode(); 
            if (!FTPReply.isPositiveCompletion(reply)) {  
                ftp.disconnect();  
            }  
            ftp.enterLocalPassiveMode();
            FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);   
            conf.setServerLanguageCode("zh"); 
            ftp.setControlEncoding(CONTROL_ENCODING);
            ftp.setFileType(FTP.ASCII_FILE_TYPE);
            ftp.setFileType(FTP.BINARY_FILE_TYPE);  
        } catch (Exception e) {
            e.printStackTrace();
        }
     }

     //上传文件
     public static void uploadFile( String basePath,String filePath, String filename, InputStream input) {  
            try {  
                //切换到上传目录  
                if (!ftp.changeWorkingDirectory(basePath+filePath)) {  
                    //如果目录不存在创建目录  
                    String[] dirs = filePath.split("/");  
                    String tempPath = basePath;  
                    for (String dir : dirs) {  
                        if (null == dir || "".equals(dir)) continue;  
                        tempPath += "/" + dir;  
                        if (!ftp.changeWorkingDirectory(tempPath)) {  
                            if (!ftp.makeDirectory(tempPath)) {  
                            } else {  
                                ftp.changeWorkingDirectory(tempPath);  
                            }  
                        }  
                    }  
                }  
                //上传文件  
                ftp.storeFile(new String(filename.getBytes("GBK"),"ISO-8859-1"), input);
                input.close();  
                ftp.logout();  
            } catch (IOException e) {  
                e.printStackTrace();  
            } finally {  
                if (ftp.isConnected()) {  
                    try {  
                        ftp.disconnect();  
                    } catch (IOException ioe) {  
                    }  
                }  
            }  
        }  

     //下载文件
     public static void downloadFile(String remotePath,String fileName, String localPath) {  
            try {  
                ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录  
                FTPFile[] fs = ftp.listFiles();  
                for (FTPFile ff : fs) {  
                    if (ff.getName().equals(fileName)) {  
                        File localFile = new File(localPath + "/" + ff.getName());  
                        OutputStream is = new FileOutputStream(localFile);  
                        ftp.retrieveFile(ff.getName(), is);  
                        is.close();  
                    }  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  

     //删除文件
     public static void deleteFtpFile( String remotePath,String fileName)  {
        try {
            // 转移到FTP服务器目录
            ftp.deleteFile(remotePath + "/" + fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //删除文件目录
    public static  void deleteFTPdir(String pathName) throws Exception {
        ftp.enterLocalPassiveMode();
        FTPFile[] files = ftp.listFiles(pathName);
        if (null != files && files.length > 0) {
            for (FTPFile file : files) {
                if (file.isDirectory()) {
                    deleteFTPdir(pathName +"/"+ file.getName());
                    ftp.changeWorkingDirectory(pathName.substring(0,pathName.lastIndexOf("/")));
                    ftp.removeDirectory(pathName);
                }else{
                    ftp.deleteFile(pathName+"/"+file.getName());
                }
            }
        }
        ftp.changeWorkingDirectory(pathName.substring(0,pathName.lastIndexOf("/")));
        ftp.removeDirectory(pathName);
    }

    //退出ftp服务
    public static void logoutFTPService() throws IOException{
        if(ftp != null){
            ftp.logout();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值