java 往ftp上上传跟下载txt文件

                         往FTP上上传跟下载txt文件。首先要封装一些上传跟下载的方法,然后通过主方法的调用封装的方法,传递相应的参数。

1:封装上传下载类代码如下:

package com.zhao.hangzhou;

import java.io.DataInputStream;     
import java.io.File;     
import java.io.FileInputStream;     
import java.io.FileOutputStream;     
import java.io.IOException;     
import java.io.OutputStream;     
import java.util.ArrayList;     
import java.util.List;     
import java.util.StringTokenizer;     
import sun.net.TelnetInputStream;     
import sun.net.TelnetOutputStream;     
import sun.net.ftp.FtpClient; 

/**   
 * ftp上传,下载   
 * @author QQ372561939 2012-11-12
 *   
 */    
public class FtpUtil {     
    
    private String ip = "";     
    
    private String username = "";     
    
    private String password = "";     
    
    private int port = -1;     
    
    private String path = "";     
    
    FtpClient ftpClient = null;     
    
    OutputStream os = null;     
    
    FileInputStream is = null;     
    
    public FtpUtil(String serverIP, String username, String password) {     
        this.ip = serverIP;     
        this.username = username;     
        this.password = password;     
    }     
         
    public FtpUtil(String serverIP, int port, String username, String password) {     
        this.ip = serverIP;     
        this.username = username;     
        this.password = password;     
        this.port = port;     
    }     
    
    /**   
     * 连接ftp服务器   
     *    
     * @throws IOException   
     */    
    public boolean connectServer(){     
        ftpClient = new FtpClient();     
        try {     
            if(this.port != -1){     
                    ftpClient.openServer(this.ip,this.port);     
            }else{     
                ftpClient.openServer(this.ip);     
            }     
            ftpClient.login(this.username, this.password);     
            if (this.path.length() != 0){     
                ftpClient.cd(this.path);// path是ftp服务下主目录的子目录                
            }     
            ftpClient.binary();// 用2进制上传、下载     
            System.out.println("已登录到\"" + ftpClient.pwd() + "\"目录");     
            return true;     
        }catch (IOException e){     
            e.printStackTrace();     
            return false;     
        }     
    }     
         
    /**   
     * 断开与ftp服务器连接   
     *    
     * @throws IOException   
     */    
    public boolean closeServer(){     
        try{     
            if (is != null) {     
                is.close();     
            }     
            if (os != null) {     
                os.close();     
            }     
            if (ftpClient != null) {     
                ftpClient.closeServer();     
            }     
            System.out.println("已从服务器断开");     
            return true;     
        }catch(IOException e){     
            e.printStackTrace();     
            return false;     
        }     
    }     
         
    /**   
     * 检查文件夹在当前目录下是否存在 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值