java实现FTP上传和下载

因路径过于复杂的情况下,在FTP上传之前需要频繁的切换工作目录,已经创建工作目录,若使用本UTIL,可以直接将服务器上的文件夹直接上传。
编码问题请根据系统不同自行修改。
注意System.getProperty(“file.encoding”)为获取程序入口文件编码,并非可以用于区分OS和Linux的判定。

package com.weavernorth.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import com.weavernorth.util.LogUtil;

/**
 * 封装FTP功能函数
 * Apace commons-net架包中的ftp工具类实现的
 * @author Dylan
 * 
 */
public class FtpCollectionUtil  {

    private FTPClient ftp;  

    private FileOutputStream fos = null;  

    private static String encoding = System.getProperty("file.encoding");

    /**
     * 
     * @param path
     *            上传到ftp服务器哪个路径下
     * @param addr
     *            地址
     * @param port
     *            端口号
     * @param username
     *            用户名
     * @param password
     *            密码
     * @return
     * @throws Exception
     */
    public boolean connect(String path, String addr, int port, String username,
            String password) throws Exception {
        System.out.println(encoding);
        boolean result = false;
        ftp = new FTPClient();
        int reply;
        ftp.connect(addr, port);
        ftp.login(username, password);
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return result;
        }
        ftp.changeWorkingDirectory(path);
        result = true;
        return result;
    }

    /**
     * 
     * @param file
     *            上传的文件或文件夹
     * @throws Exception
     */
    public void upload(File file) throws Exception {
        if (file.isDirectory()) {
            System.out.println(file.getName());
            boolean b = ftp.makeDirectory(new String(file.getName().getBytes("GBK"),"iso-8859-1"));
            System.out.println(b);
            boolean flag = ftp.changeWorkingDirectory(new String(file.getName().getBytes("GBK"),"iso-8859-1"));
            System.out.println("  --file.getName()---" + file.getName());
            System.out.println("  --flag---" + flag);
            String[] files = file.list();
            for (int i = 0; i < files.length; i++) {
                File file1 = new File(file.getPath() + "\\" + files[i]);
                System.out.println(file.getPath() + "\\" + files[i]);
                if (file1.isDirectory()) {
                    upload(file1);
                    ftp.changeToParentDirectory();
                } else {
                    System.out.println("文件名" + file.getPath() + "\\" + files[i]);
                    File file2 = new File(file.getPath() + "\\" + files[i]);
                    FileInputStream input = new FileInputStream(file2);
                    System.out.println("文件名name" + file2.getName());
                    ftp.storeFile(new String(
                            file2.getName().getBytes("GBK"), "iso-8859-1"),
                            input);
                    input.close();
                }
            }
        } else {
            File file2 = new File(file.getPath());
            FileInputStream input = new FileInputStream(file2);
            ftp.storeFile(new String(file2.getName().getBytes("GBK"),
                    "iso-8859-1"), input);
            input.close();
        }
    }

    public void downFileOrDir(String ftpFileName, String localDir) {
        try {
            File file = new File(ftpFileName);

            File temp = new File(localDir);

            if (!temp.exists()) {
                temp.mkdirs();
            }
            // 判断是否是目录
            if (isDir(ftpFileName)) {
                String[] names = ftp.listNames();
                for (int i = 0; i < names.length; i++) {
                    System.out.println(names[i] + "^^^^^^^^^^^^^^");
                    if (isDir(names[i])) {
                        downFileOrDir(ftpFileName + '/' + names[i], localDir
                                + File.separator + names[i]);
                        ftp.changeToParentDirectory();
                    } else {
                        File localfile = new File(localDir + File.separator
                                + names[i]);
                        if (!localfile.exists()) {
                            fos = new FileOutputStream(localfile);
                            ftp.retrieveFile(names[i], fos);

                        } else {
                            System.out.println("开始删除文件");
                            file.delete();
                            System.out.println("文件已经删除");
                            fos = new FileOutputStream(localfile);
                            ftp.retrieveFile(ftpFileName, fos);

                        }

                    }
                }
            } else {

                File localfile = new File(localDir + File.separator
                        + file.getName());
                if (!localfile.exists()) {
                    fos = new FileOutputStream(localfile);
                    ftp.retrieveFile(ftpFileName, fos);

                } else {
                    System.out.println("开始删除文件");
                    file.delete();
                    System.out.println("文件已经删除");
                    fos = new FileOutputStream(localfile);
                    ftp.retrieveFile(ftpFileName, fos);

                }
                ftp.changeToParentDirectory();

            }

            System.out.println("下载成功!");
        } catch (SocketException e) {
            System.out.println("连接失败!" + e);
        } catch (IOException e) {
            System.out.println("下载失败!" + e);
        }

    }

    // 判断是否是目录
    public boolean isDir(String fileName) {
        try {
            // 切换目录,若当前是目录则返回true,否则返回true。
            boolean falg = ftp.changeWorkingDirectory(fileName);
            return falg;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e);
        }

        return false;
    }

    /**
     * 关闭FTP连接
     * 
     * @throws IOException
     */
    public void disConnection() throws IOException {
        // 检验ftp是否连接---如果连接关闭连接
        if (this.ftp.isConnected()) {
            this.ftp.disconnect();
        }
    }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值