ftp操作

1、ftp实体bean
package org.imos.cfdispatch.util.ftp;


public class FTPEntityBean {
	
	String hostName;
	String userName;
	String password;
	String port;
	String DownloadPath;
	String SaveFilePath;
	
	public FTPEntityBean(String hostName, String userName, String password,
			String port, String downloadPath, String saveFilePath) {
		super();
		this.hostName = hostName;
		this.userName = userName;
		this.password = password;
		this.port = port;
		DownloadPath = downloadPath;
		SaveFilePath = saveFilePath;
	}
	public String getDownloadPath() {
		return DownloadPath;
	}
	public void setDownloadPath(String downloadPath) {
		DownloadPath = downloadPath;
	}
	public String getHostName() {
		return hostName;
	}
	public void setHostName(String hostName) {
		this.hostName = hostName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPort() {
		return port;
	}
	public void setPort(String port) {
		this.port = port;
	}
	public String getSaveFilePath() {
		return SaveFilePath;
	}
	public void setSaveFilePath(String saveFilePath) {
		SaveFilePath = saveFilePath;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	
}

2.ftp连接

package org.imos.cfdispatch.util.ftp;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;



public class FtpLogin {
	/**
	 * 
	* @Title: connectFTP 
	* @Description:连接FTP服务器
	* @param @param ftpbean
	* @param @return    设定文件 
	* @return FTPClient    返回类型 
	* @throws
	 */
    public static FTPClient connectFTP(FTPEntityBean ftpbean) {
        FTPClient downloadFtp = new FTPClient();
        String hostName=ftpbean.getHostName();
        String userName=ftpbean.getUserName();
        String password=ftpbean.getPassword();
        String port=ftpbean.getPort();
        int reply;
        try {
            downloadFtp.setDataTimeout(2000);
            downloadFtp.connect(hostName, Integer.parseInt(port));
            reply = downloadFtp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                downloadFtp.disconnect();
                return null;
            }
            if (!downloadFtp.login(userName, password)) {
                downloadFtp.logout();
                return null;
            }
        } catch (SocketException ex) {
//            Log.error("登录FTP失败");
            ex.printStackTrace();
            downloadFtp=null;
        } catch (IOException e) {
            e.printStackTrace();
//        	    Log.error("登录FTP失败");
                downloadFtp=null;
            if (downloadFtp.isConnected()) {
                try {
                    downloadFtp.disconnect();
                } catch (IOException f) {
                    return null;
                }
            }
        }
        return downloadFtp;
    }
    
    public void getFTPProper(){
    	
    }
    

    
}

3.ftp上传

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.imos.cfdispatch.util.ftp;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;

public class FtpUpload {
    String hostName = "192.168.0.123";

    String userName = "test";

    String password = "test";

    String remoteDir = "aaa";
    String port = "21";
    FTPEntityBean ftpbean;
    public FtpUpload(){

    }
    /**
     * 
    * <p>Title: </p> 
    * <p>FTP连接信息 </p
    * @param ftpbean
     */
    public FtpUpload(FTPEntityBean ftpbean) {
        this.hostName=ftpbean.hostName;
        this.userName=ftpbean.getUserName();
        this.password=ftpbean.getPassword();
        this.port=ftpbean.getPort();
        this.remoteDir=ftpbean.getSaveFilePath();
        this.ftpbean=ftpbean;
    }
/**
 * 
* @Title: Upload 
* @Description: 数据上传
* @param @param localfilename 本地文件路径
* @param @param remotefilename  上传文件路径
* @return void    返回类型 
* @throws
 */
    public void Upload(String localfilename, String remotefilename){
       
        ftpbean.setHostName(remotefilename);
        if(UploadData(ftpbean,localfilename,remotefilename)){
        }else{

        }

    }
    /**
     * 
    * @Title: UploadData 
    * @Description: 数据上传
    * @param @param ftpbean
    * @param @param localfilename
    * @param @param remotefilename
    * @param @return    设定文件 
    * @return boolean    返回类型 
    * @throws
     */

    public boolean UploadData(FTPEntityBean ftpbean,String localfilename, String remotefilename){

        try {
              FTPClient ftp = FtpLogin.connectFTP(ftpbean);
              if(ftp==null)return false;
            InputStream input = new FileInputStream(localfilename);
            ftp.storeFile(remotefilename, input);
            ftp.disconnect();
            return true;
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            return false;
        }catch(IOException ex){
            ex.printStackTrace();
            return false;
        }


    }


    public static void main(String args[]) throws Exception {
       
    }

}


4.ftp下载

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.imos.cfdispatch.util.ftp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;


import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class FtpDownload {
	/**
	 * 
	* @Title: downloadFile 
	* @Description: 数据下传
	* @param @param ftpbean
	* @param @return    设定文件 
	* @return boolean    返回类型 
	* @throws
	 */
    public boolean downloadFile(FTPEntityBean ftpbean) {
        FTPClient ftp = FtpLogin.connectFTP(ftpbean);
        boolean result = false;
        if (ftp != null) {
            String filename=null;
            try {
                ftp.changeWorkingDirectory(ftpbean.getDownloadPath());
                FTPFile[] filelist = ftp.listFiles();                
                for (FTPFile file : filelist) {
                    if (file.isFile()) {
                        filename = file.getName();   
                        filename= new String(filename.getBytes(),"UTF-8");
                            System.out.println("正在下载文件" + filename + ",请等待....");                        
                            File fi = new File(ftpbean.getSaveFilePath() + filename);
                            FileOutputStream fos = new FileOutputStream(fi);
                            ftp.retrieveFile(filename, fos);                 
                            fos.close();                     
                            System.out.println("下载文件" + filename + ",完成!");                        
                            result = true;

                    }
                }
                if(!result){
                    System.out.println("没有的文件!");   
                }
                ftp.logout();
            } catch (IOException e) {
                System.out.println("下载文件" + filename + ",失败!" + e.getMessage());
                result = false;
            }
        } else {
            result = false;
        }
        return result;
    }
    
    

}


 



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值