com.webarch.common.net.ftp.FtpService.java Source code

Java tutorial

Introduction

Here is the source code for com.webarch.common.net.ftp.FtpService.java

Source

/*
    Copyright  DR.YangLong
    
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    
    http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

package com.webarch.common.net.ftp;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * package: common.net.ftp </br>
 * functional describe:Ftp?ftp
 *
 * @author DR.YangLong [410357434@163.com]
 * @version 1.0    2015/7/2 20:19
 */
public class FtpService {
    private static final Logger logger = LoggerFactory.getLogger(FtpService.class);
    //Linuxftp?
    private static final int DEFAULT_PORT = 21;
    //ftp?
    private String userName;
    //ftp?
    private String password;
    //ftp?
    private int port = DEFAULT_PORT;
    //ftp??
    private String url;

    /**
     * 
     *
     * @param fileName   ??
     * @param path       ftp?
     * @param fileStream ?
     * @return true/false ?
     */
    public boolean uploadFile(String fileName, String path, InputStream fileStream) {
        boolean success = false;
        FTPClient ftpClient = new FTPClient();
        try {
            int replyCode;
            ftpClient.connect(url, port);
            ftpClient.login(userName, password);
            replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                return false;
            }
            ftpClient.changeWorkingDirectory(path);
            ftpClient.storeFile(fileName, fileStream);
            fileStream.close();
            ftpClient.logout();
            success = true;
        } catch (IOException e) {
            logger.error("ftp?", e);
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    logger.error("ftp?", e);
                }
            }
        }
        return success;
    }

    /**
     * 
     *
     * @param remotePath ftp
     * @param fileName   ???
     * @param localPath  ???
     * @return true/false ?
     */
    public boolean downloadFile(String remotePath, String fileName, String localPath) {
        boolean success = false;
        FTPClient ftpClient = new FTPClient();
        try {
            int replyCode;
            ftpClient.connect(url, port);
            ftpClient.login(userName, password);
            replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                return false;
            }
            ftpClient.changeWorkingDirectory(remotePath);
            FTPFile[] files = ftpClient.listFiles();
            for (FTPFile file : files) {
                if (file.getName().equals(fileName)) {
                    File localFile = new File(localPath + File.separator + file.getName());
                    OutputStream outputStream = new FileOutputStream(localFile);
                    ftpClient.retrieveFile(file.getName(), outputStream);
                    outputStream.close();
                }
            }
            ftpClient.logout();
            success = true;
        } catch (IOException e) {
            logger.error("ftp?", e);
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    logger.error("ftp?", e);
                }
            }
        }
        return success;
    }
}