cn.zhuqi.mavenssh.web.util.test.FtpTest.java Source code

Java tutorial

Introduction

Here is the source code for cn.zhuqi.mavenssh.web.util.test.FtpTest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.zhuqi.mavenssh.web.util.test;

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.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

/**
 *
 * @author Administrator
 */
public class FtpTest {

    private FTPClient ftpClient;

    /**
     *
     * @param path ftp?
     * @param addr ?
     * @param port ??
     * @param username ??
     * @param password ?
     * @return
     * @throws Exception
     */
    private boolean connect(String path, String addr, int port, String username, String password) throws Exception {
        boolean result = false;
        ftpClient = new FTPClient();
        int reply;
        ftpClient.connect(addr, port);
        ftpClient.login(username, password);
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            return result;
        }
        ftpClient.changeWorkingDirectory(path);
        result = true;
        return result;
    }

    /**
     *
     * @param file 
     * @throws Exception
     */
    private void upload(File file) throws Exception {
        if (file.isDirectory()) {
            ftpClient.makeDirectory(file.getName());
            ftpClient.changeWorkingDirectory(file.getName());
            String[] files = file.list();
            for (int i = 0; i < files.length; i++) {
                File file1 = new File(file.getPath() + "\\" + files[i]);
                if (file1.isDirectory()) {
                    upload(file1);
                    ftpClient.changeToParentDirectory();
                } else {
                    File file2 = new File(file.getPath() + "\\" + files[i]);
                    FileInputStream input = new FileInputStream(file2);
                    ftpClient.storeFile(file2.getName(), input);
                    input.close();
                }
            }
        } else {
            File file2 = new File(file.getPath());
            FileInputStream input = new FileInputStream(file2);
            ftpClient.storeFile(file2.getName(), input);
            input.close();
        }
    }

    /**
     * Description: ?FTP?
     *
     * @param url FTP?hostname
     * @param port FTP???-1
     * @param username FTP?
     * @param password FTP?
     * @param path FTP??
     * @param filename FTP???
     * @param input ?
     * @return ?true?false
     */
    public static boolean uploadFile(String url, int port, String username, String password, String path,
            String filename, InputStream input) {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            // FTP?
            if (port > -1) {
                ftp.connect(url, port);
            } else {
                ftp.connect(url);
            }
            // FTP
            ftp.login(username, password);
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return success;
            }
            ftp.changeWorkingDirectory(path);
            ftp.storeFile(filename, input);

            input.close();
            ftp.logout();
            success = true;
        } catch (IOException e) {
            success = false;
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException e) {
                }
            }
        }
        return success;
    }

    /**
     * Description: FTP?
     *
     * @Version1.0
     * @param url FTP?hostname
     * @param port FTP??
     * @param username FTP?
     * @param password FTP?
     * @param remotePath FTP?
     * @param fileName ???
     * @param localPath ??
     * @return
     */
    public static boolean downloadFile(String url, int port, String username, String password, String remotePath,
            String fileName, String localPath) {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            // FTP?
            if (port > -1) {
                ftp.connect(url, port);
            } else {
                ftp.connect(url);
            }
            ftp.login(username, password);//
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return success;
            }
            ftp.changeWorkingDirectory(remotePath);//FTP?
            FTPFile[] fs = ftp.listFiles();
            for (FTPFile ff : fs) {
                if (ff.getName().equals(fileName)) {
                    File localFile = new File(localPath + "/" + ff.getName());
                    OutputStream is = new FileOutputStream(localFile);
                    ftp.retrieveFile(ff.getName(), is);
                    is.close();
                }
            }

            ftp.logout();
            success = true;
        } catch (IOException e) {
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException e) {
                }
            }
        }
        return success;
    }

    private void closeConnect() {
        try {
            ftpClient.disconnect();
            System.out.println("disconnect success");
        } catch (IOException ex) {
            System.out.println("not disconnect");
            throw new RuntimeException(ex);
        }
    }

    public static void main(String[] args) throws Exception {
        //  FtpTest t = new FtpTest();
        //  t.connect("", "localhost", 21, "zhuqi125", "dacm409");
        //  File file = new File("D:\\student\\201355\\2013551015.jpg");
        //  t.upload(file);
        //  t.closeConnect();
        File file = new File("D:\\student\\201355\\2013551015.jpg");
        FileInputStream input = new FileInputStream(file);
        FtpTest.uploadFile("192.168.0.119", 21, "zhuqi125", "dacm409", "", "1.jpg", input);
        FtpTest.downloadFile("192.168.0.119", 21, "zhuqi125", "dacm409", "", "1.jpg", "C:\\");
    }
}