apache方式ftp文件操作

package util;

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 org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;


public class FtpManager {

    private static final Logger log = Logger.getLogger(FtpManager.class);

    String localfilename;
    String remotefilename;
    public FTPClient ftpClient;
    private boolean result;// 登录是否成功
    //ftp超时时间
    private static int ftptimes = Integer.parseInt(Configuration.readConfigString("ftptimes", "config"));

    //连接到ftp服务
    public FtpManager(String Ip, int Port, String Ftpname, String Ftppass, String path) {
        connectServer(Ip, Port, Ftpname, Ftppass, path);
    }

    /**
     * 连接上ftp服务器
     * 
     * @param ip
     * @param port
     * @param user用户名
     * @param password密码
     * @param path服务器上的路径
     */
    public boolean connectServer(String Ip, int Port, String Ftpname, String Ftppass, String path) {
        try {
            ftpClient = new FTPClient();
            ftpClient.setConnectTimeout(ftptimes);
            ftpClient.connect(Ip, Port);
            ftpClient.login(Ftpname, Ftppass);
            result = true;
            if (path.length() != 0) {
                ftpClient.changeWorkingDirectory(path);
            }
             // 设置以二进制方式传输  
            ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);  
            log.debug("ftp[" + Ip + "]登录成功:");
        } catch (Exception ex) {
            result = false;
            log.error("ftp[" + Ip + "]登录失败:");
        }
        return result;
    }

    /**
     * 把远程系统上的目录切换到参数remoteDirectory所指定的目录
     * 
     * @param remoteDirectory
     */
    public void cd(String remoteDirectory) throws IOException {
        ftpClient.changeWorkingDirectory(remoteDirectory);
    }

    /**
     * 在远端系统浏览上一级目录
     * 
     * @param remoteDirectory
     */
    public void cdup() throws IOException {
        ftpClient.changeToParentDirectory();
    }

    /**
     * 发送命令 cmd给服务器
     * 
     * @param cmd
     * @throws IOException 
     */
    public void sendServer(String cmd) throws IOException {
        ftpClient.sendSiteCommand(cmd);
    }

    /**
     * 关闭ftp连接
     */
    public void closeConnect() {
        try {
            ftpClient.disconnect();
        } catch (IOException ex) {
            log.error("断开FTP服务器失败", ex);
        }
    }

    /**
     * 上传到ftp服务器
     * 
     * @throws Exception
     */
    public boolean upload(String remoteFile, String localFile) {
        boolean result = false;
        OutputStream os = null;
        java.io.File file_in = new java.io.File(localFile);
        FileInputStream is = null;
        try {
            log.debug(localFile + "FTP文件开始上传。。。。。");
            os = ftpClient.storeFileStream(remoteFile);
            is = new FileInputStream(file_in);
            byte[] bytes = new byte[4096];
            int c;
            while ((c = is.read(bytes)) != -1) {
                os.write(bytes, 0, c);
            }
            log.debug(localFile + "FTP文件上传成功");
            result = true;
        } catch (Exception ex) {
            result = false;
            log.error(localFile + ",FTP上传文件失败", ex);
        } finally {
            try {
                if (is != null)
                    is.close();
                if (os != null)
                    os.close();
            } catch (IOException e) {
                log.error("关闭流失败", e);
            }
        }
        log.debug(localFile + "FTP文件结束上传。。。。。结果:" + result);
        return result;
    }

    /**
     * 指定路径从ftp服务器下载
     * 
     * @param remoteFile
     * @param localFile
     */
    public void download(String remoteFile, String localFile) throws Exception {
        try {
            InputStream is = ftpClient.retrieveFileStream(remoteFile);
            File file_in = new File(localFile);
            FileOutputStream os = new FileOutputStream(file_in);
            byte[] bytes = new byte[1024];
            int c;
            while ((c = is.read(bytes)) != -1) {
                os.write(bytes, 0, c);
            }
            os.close();
            is.close();
        } catch (IOException ex) {
            log.error("FTP下载文件失败", ex);
        }
    }

    /** 
     * 判断Ftp目录是否存在,如果不存在则创建目录 
     * */
    public void isDirExist(String dir) {
        try {
            if (!ftpClient.changeWorkingDirectory(dir)) {
                if (ftpClient.makeDirectory(dir)) {
                    ftpClient.changeWorkingDirectory(dir);
                }
            }
        } catch (Exception e) {
            log.error("目录创建失败", e);
        }
    }

    public static void main(String agrs[]) {
        try {
            FtpManager f = new FtpManager("125.88.109.47", 21, "hzdracom","hzdracom", "");
            // 获取文件列表
//          f.isDirExist("upload/hftest");
            f.upload("hftest/hf/wppp.tif", "d:\\mcs\\201502\\4154720150211101913.tif");
            f.closeConnect();
            System.out.println("=====================");
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    public boolean isResult() {
        return result;
    }

    public void setResult(boolean result) {
        this.result = result;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我要修改昵称

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值