common-net ftp简单demo

package cn.xt.net.ftp;  
  
import java.io.File;  
import java.io.FileInputStream;  
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;  
import org.junit.Test;  
  
public class Ftpclient {  
      
    final static String PATH="d:/zhanghuadong/";  
    final static String SERVICE_ADDRESS="192.168.0.115";  
    final static String USER="ftpuser";  
    final static String PASSWORD="ftpuser";  
    final static String DOWN_FILE_NAME="XF200070750845720090218";  
    /** 
     * Ftp下载 
     * 
     */  
    @Test  
    public void ftpQuery(){  
        FTPClient client=new FTPClient();  
        try {  
            client.connect(SERVICE_ADDRESS);  
            boolean islogon=client.login(USER, PASSWORD);  
            FTPFile[] files=null;  
            if(islogon){  
                int reply=client.getReplyCode();  
                /**判断返回代码 */  
                if(!FTPReply.isPositiveCompletion(reply)){  
                    throw new Exception("对不起,"+SERVICE_ADDRESS+" user:"+USER+" password:"+PASSWORD+" 连接出错!");  
                }  
                /**更改下载的目录 */  
                client.changeWorkingDirectory("qyyd_fsend/");  
                files=client.listFiles();  
                File file=null;  
                for (int i = 0; i < files.length; i++) {  
                    System.out.println(files[i].getName());  
                    if(files[i].getName().equals("XF200070750845720090218")){  
                        System.out.println("down...");  
                        file=new File(PATH+files[i].getName());  
                    }  
                }  
                if(file!=null){  
                    OutputStream out=new FileOutputStream(file);  
                    client.retrieveFile("XF200070750845720090218", out);  
                    System.out.println("down success...");  
                    out.close();  
                }  
                System.out.println("logon 成功!");  
            }  
            client.logout();  
            client.disconnect();  
        } catch (SocketException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } catch(Exception e){  
            e.printStackTrace();  
        }  
    }  
      
    public void ftpUpload(){  
        FTPClient ftp=new FTPClient();  
        InputStream stream=null;  
        try{  
            //ftp.setDefaultPort(221);  
            ftp.connect(SERVICE_ADDRESS);  
            boolean islogon=ftp.login(USER, PASSWORD);  
            if(islogon){  
                int recode=ftp.getReplyCode();  
                if(!FTPReply.isPositiveCompletion(recode)){  
                    throw new Exception("对不起,"+SERVICE_ADDRESS+" user:"+USER+" password:"+PASSWORD+" 连接出错!");  
                }  
                stream=new FileInputStream(new File(PATH+DOWN_FILE_NAME));  
                FTPFile[] files=ftp.listFiles();  
                int overname=0;  
                for (int i = 0; i < files.length; i++) {  
                    if(files[i].getName().indexOf(DOWN_FILE_NAME)!=-1){  
                        overname++;  
                    }  
                }  
                if(overname>0){  
                    ftp.rename(DOWN_FILE_NAME, DOWN_FILE_NAME+"."+overname);  
                }  
                //设置文件类型 文本类型 二进制文件 区别文本会根据系统加换行符  二进制则不会  
                ftp.setFileType(FTPClient.ASCII_FILE_TYPE);  
                boolean isstore=ftp.storeFile(DOWN_FILE_NAME,stream);  
                stream.close();  
                if(!isstore){  
                    System.out.println("upload fail ...");  
                }  
                System.out.println("upload  success...");  
            }  
        }catch(Exception e){  
            e.printStackTrace();  
        }finally{  
            try {  
                ftp.logout();  
                ftp.disconnect();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
      
    public static void main(String[] args){  
        Ftpclient client=new Ftpclient();  
        client.ftpUpload();  
    }  
}  


你可能感兴趣的:(common-net ftp简单demo)