ru.in360.FTPUtil.java Source code

Java tutorial

Introduction

Here is the source code for ru.in360.FTPUtil.java

Source

/*
 * This file is part of in360TourBuilder.
 *
 *     in360TourBuilder 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.
 *
 *     in360TourBuilder 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    ? in360TourBuilder.
 *
 *    in360TourBuilder - ?? :   ???  /
 *    ?   ??  ?  GNU   ,
 *          ?  ??;
 *     ? 3 ,  (  )   
 *    ?.
 *
 *    in360TourBuilder ?????  ,    ,
 *       ???;   ?  ?? ?
 *     ?  ?? .  ?.  
 *    ?  GNU.
 *
 *          ?  GNU
 *    ? ? ? . ? ?  , ?.
 *    <http://www.gnu.org/licenses/>.
 *
 * 06.11.14 1:47 Anton Fomchenko 360@in360.ru
 */

package ru.in360;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * This utility class implements method for uploading a whole directory from
 * local computer to a remote FTP server, based on Apache Commons Net library.
 *
 * @author www.codejava.net
 */
public class FTPUtil {

    //    public static void main(String[] args) throws IOException {
    //        saveFilesToServer("/tezt", new File("E:/winhex"));
    //    }

    public static void saveFilesToServer(FTPClient ftp, String remoteDest, File localSrc) throws IOException {
        //        FTPClient ftp = new FTPClient();
        //
        //        ftp.connect("46.8.19.232", 21);
        //
        //
        //        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
        //            ftp.disconnect();
        //            System.out.println("FTP not disconnected");
        //        }
        //        ftp.login("admin", "fa43limited");
        //        ftp.enterLocalPassiveMode();
        System.out.println("Connected to server .");

        System.out.println("remote directory: " + remoteDest);
        ftp.changeWorkingDirectory(remoteDest);
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

        System.out.println("upload: " + localSrc.getPath());
        upload(localSrc, ftp);
        System.out.println(ftp.getReplyString());

    }

    public static void upload(File src, FTPClient ftp) throws IOException {
        if (src.isDirectory()) {
            ftp.makeDirectory(src.getName());
            ftp.changeWorkingDirectory(src.getName());
            for (File file : src.listFiles()) {
                upload(file, ftp);
            }
            ftp.changeToParentDirectory();
        } else {
            InputStream srcStream = null;
            try {
                srcStream = src.toURI().toURL().openStream();
                ftp.storeFile(src.getName(), srcStream);
            } finally {
                IOUtils.closeQuietly(srcStream);
            }
        }
    }
}