FTP 帮助类

/*
 * Created on 2011-4-21
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.pg.commerce.utils;

import sun.net.ftp.*;  
import sun.net.*;  
import java.io.*;  
 

public class FtpTool {  
    String ip;  
    int port;  
    String user;  
    String pwd;  
    String remotePath;  
    String localPath;  
    FtpClient ftpClient;  
 
    /** 
     * 连接ftp服务器
     * @param ip 
     * @param port 
     * @param user 
     * @param pwd 
     * @return 
     * @throws Exception 
     */ 
    public boolean connectServer(String ip, int port, String user, String pwd)  
        throws Exception {  
        boolean isSuccess = false;  
        try {  
            ftpClient = new FtpClient();  
            ftpClient.openServer(ip, port);  
            ftpClient.login(user, pwd);  
            isSuccess = true;  
        } catch (Exception ex) {  
            throw new Exception("Connect ftp server error:" + ex.getMessage());  
        }  
        return isSuccess;  
    }  
 
    /** 
     * 下载文件 
     * @param remotePath 
     * @param localPath 
     * @param filename 
     * @throws Exception 
     */ 
    public void downloadFile(String remotePath,String localPath, String filename) throws Exception {  
        try {  
            if (connectServer(getIp(), getPort(), getUser(), getPwd())) {  
                if (remotePath.length() != 0) 
                 
                 
                 
                    ftpClient.cd(remotePath);  
                ftpClient.binary();  
                TelnetInputStream is = ftpClient.get(filename);  
                File file_out = new File(localPath + File.separator + filename);  
                FileOutputStream os = new FileOutputStream(file_out);  
                byte[] bytes = new byte[1024];  
                int c;  
                while ((c = is.read(bytes)) != -1) {  
                    os.write(bytes, 0, c);  
                }  
                is.close();  
                os.close();  
                ftpClient.closeServer();  
            }  
        } catch (Exception ex) {  
            throw new Exception("ftp download file error:" + ex.getMessage());  
        }  
    }  
 
    /** 
     * 上传文件 
     * @param remotePath 
     * @param localPath 
     * @param filename 
     * @throws Exception 
     */ 
    public void uploadFile(String remotePath,String localPath, String filename) throws Exception {  
        try {  
            if (connectServer(getIp(), getPort(), getUser(), getPwd())) {  
                if (remotePath.length() != 0)  
                 System.out.println(ftpClient.pwd());
                ftpClient.cd(ftpClient.pwd()); 
                ftpClient.cd(remotePath);  
                ftpClient.binary();  
//                ftpClient.sendServer("PASV");
                TelnetOutputStream os = ftpClient.put(filename);  
                File file_in = new File(localPath + File.separator + filename);  
                FileInputStream is = new FileInputStream(file_in);  
                byte[] bytes = new byte[1024];  
                int c;  
                while ((c = is.read(bytes)) != -1) {  
                    os.write(bytes, 0, c);  
                }  
                is.close();  
                os.close();  
                ftpClient.closeServer();  
            }  
        } catch (Exception ex) {  
         ex.printStackTrace();
            throw new Exception("ftp upload file error:" + ex.getMessage());  
        }  
    }  
 
   
   
    public void uploadFileForNoraml(String remotePath,String localPath, String filename) throws Exception {  
        try {  
            if (connectServer(getIp(), getPort(), getUser(), getPwd())) {  
                if (remotePath.length() != 0)  
                 System.out.println(ftpClient.pwd());
                ftpClient.cd(ftpClient.pwd()); 
                ftpClient.cd(remotePath);  
                ftpClient.binary();  
//                ftpClient.sendServer("PASV");
                TelnetOutputStream os = ftpClient.put(filename);  
                File file_in = new File(localPath + File.separator + filename);  
                FileInputStream is = new FileInputStream(file_in);  
                byte[] bytes = new byte[1024];  
                int c;  
                while ((c = is.read(bytes)) != -1) {  
                    os.write(bytes, 0, c);  
                }  
                is.close();  
                os.close();  
                ftpClient.closeServer();  
            }  
        } catch (Exception ex) {  
         ex.printStackTrace();
            throw new Exception("ftp upload file error:" + ex.getMessage());  
        }  
    }  
   
    /** 
     * @return 
     */ 
    public String getIp() {  
        return ip;  
    }  
 
    /** 
     * @return 
     */ 
    public int getPort() {  
        return port;  
    }  
 
    /** 
     * @return 
     */ 
    public String getPwd() {  
        return pwd;  
    }  
 
    /** 
     * @return 
     */ 
    public String getUser() {  
        return user;  
    }  
 
    /** 
     * @param string 
     */ 
    public void setIp(String string) {  
        ip = string;  
    }  
 
    /** 
     * @param i 
     */ 
    public void setPort(int i) {  
        port = i;  
    }  
 
    /** 
     * @param string 
     */ 
    public void setPwd(String string) {  
        pwd = string;  
    }  
 
    /** 
     * @param string 
     */ 
    public void setUser(String string) {  
        user = string;  
    }  
 
    /** 
     * @return 
     */ 
    public FtpClient getFtpClient() {  
        return ftpClient;  
    }  
 
    /** 
     * @param client 
     */ 
    public void setFtpClient(FtpClient client) {  
        ftpClient = client;  
    }  
 
    /** 
     * @return 
     */ 
    public String getRemotePath() {  
        return remotePath;  
    }  
 
    /** 
     * @param string 
     */ 
    public void setRemotePath(String string) {  
        remotePath = string;  
    }  
 
    /** 
     * @return 
     */ 
    public String getLocalPath() {  
        return localPath;  
    }  
 
    /** 
     * @param string 
     */ 
    public void setLocalPath(String string) {  
        localPath = string;  
    }  
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值