org.apdplat.platform.util.FtpUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.apdplat.platform.util.FtpUtils.java

Source

/**
 * 
 * APDPlat - Application Product Development Platform
 * Copyright (c) 2013, ??, yang-shangchuan@qq.com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package org.apdplat.platform.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apdplat.platform.log.APDPlatLogger;
import org.apdplat.platform.log.APDPlatLoggerFactory;
import org.springframework.stereotype.Service;

/**
 * FTP?
 * @author ??
 */
@Service
public class FtpUtils {
    protected final APDPlatLogger LOG = APDPlatLoggerFactory.getAPDPlatLogger(getClass());

    private FTPClient ftpClient;

    /**
     * FTP?
     * @param host FTP??
     * @param port FTP???
     * @param username ??
     * @param password ?
     * @return
     */
    public boolean connect(String host, int port, String username, String password) {
        try {
            ftpClient = new FTPClient();
            ftpClient.connect(host, port);
            ftpClient.login(username, password);
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            int reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftpClient.disconnect();
                LOG.error("FTP???" + reply);
                return false;
            }
        } catch (IOException e) {
            LOG.error("FTP?", e);
            return false;
        }
        return true;
    }

    /**
     * ?
     * @param file 
     * @param path ftp?
     * @return 
     */
    public boolean uploadTo(File file, String path) {
        //????
        //?
        LOG.info(" " + file.getAbsolutePath() + " ? " + path);
        try {
            String[] segs = path.split("/");
            for (String seg : segs) {
                if (!ftpClient.changeWorkingDirectory(seg)) {
                    ftpClient.makeDirectory(seg);
                    if (!ftpClient.changeWorkingDirectory(seg)) {
                        LOG.error("??:" + seg);
                        return false;
                    }
                }
            }
        } catch (IOException e) {
            LOG.error("??", e);
            return false;
        }
        return upload(file);
    }

    /**
     * 
     * @param file 
     * @return ??
     */
    private boolean upload(File file) {
        try {
            if (file.isDirectory()) {
                ftpClient.makeDirectory(file.getName());
                ftpClient.changeWorkingDirectory(file.getName());
                File[] subFiles = file.listFiles();
                for (File subFile : subFiles) {
                    if (subFile.isDirectory()) {
                        upload(subFile);
                        ftpClient.changeToParentDirectory();
                    } else {
                        try (FileInputStream input = new FileInputStream(subFile)) {
                            ftpClient.storeFile(subFile.getName(), input);
                        }
                    }
                }
            } else {
                try (FileInputStream input = new FileInputStream(file)) {
                    ftpClient.storeFile(file.getName(), input);
                }
            }
        } catch (IOException e) {
            LOG.error("", e);
            return false;
        }
        return true;
    }

    public static void main(String[] args) throws Exception {
        FtpUtils ftp = new FtpUtils();
        //File file = new File("C:\\apache-ant-1.8.4");
        //File file = new File("C:\\Users\\ysc\\Downloads\\2014-01-30-07-22-50.zip");
        File file = new File("C:\\Users\\ysc\\Downloads\\OperateLogAspect.java");
        if (ftp.connect("localhost", 21, "admin", "test")) {
            if (ftp.uploadTo(file, "java/apdplat/core")) {
                System.out.println("?");
            } else {
                System.out.println("");
            }
        }
    }
}