com.usefullc.platform.common.utils.FtpUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.usefullc.platform.common.utils.FtpUtils.java

Source

package com.usefullc.platform.common.utils;

/*
 * Copyright 2010-2011 ESunny.com All right reserved. This software is the confidential and proprietary information of
 * ESunny.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with ESunny.com.
 */

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

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

/**
 * FtpUtils.java??ftp
 * 
 * @author shengshang.tang 201448 ?10:51:35
 */
public class FtpUtils {

    private static final Logger log = LoggerFactory.getLogger(FtpUtils.class);

    private static FTPClient ftpClient = new FTPClient();

    private static String host;

    private static int port;

    private static String user;

    private static String password;

    private static String remoteDir;

    static {
        host = ConfigUtils.getValue("ftp.host");
        String cfPort = ConfigUtils.getValue("ftp.port");
        port = Integer.valueOf(cfPort);
        user = ConfigUtils.getValue("ftp.user");
        password = ConfigUtils.getValue("ftp.password");
        remoteDir = ConfigUtils.getValue("ftp.remoteDir");
    }

    /**
     * 
     * 
     * @param oppositePath 
     * @param downFileName  ??
     * @return
     */
    public static InputStream download(String oppositePath, String downFileName) {
        InputStream is = null;
        try {
            // // 
            ftpClient.connect(host, port);

            // 
            ftpClient.login(user, password);

            // ??
            int reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                log.error("FTP server refused connection,host=" + host + ",user=" + user + "," + password);
                return is;
            }

            FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
            conf.setServerLanguageCode("zh");
            String sep = "/";
            String workRemoteDir = StrUtils.joinEmpty(remoteDir, sep, oppositePath);
            FTPFile[] remoteFiles = ftpClient.listFiles(workRemoteDir);
            if (remoteFiles != null) {
                for (int i = 0; i < remoteFiles.length; i++) {
                    String name = remoteFiles[i].getName();
                    if (!name.equals(downFileName)) {
                        continue;
                    }
                    //                    String sep = File.separator;
                    String fileName = StrUtils.joinEmpty(remoteDir, sep, oppositePath, sep, name);
                    is = ftpClient.retrieveFileStream(fileName);
                }
            }
            ftpClient.logout();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                ftpClient.disconnect();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        return is;
    }

    /**
     * 
     * 
     * @param oppositePath 
     * @param uploadFileName ??
     * @param is ?
     */
    public static void upload(String oppositePath, String uploadFileName, InputStream is) {
        try {
            // 
            ftpClient.connect(host, port);

            // 
            ftpClient.login(user, password);

            // ??
            int reply = ftpClient.getReplyCode();

            if (!FTPReply.isPositiveCompletion(reply)) {
                log.error("FTP server refused connection,host=" + host + ",user=" + user + "," + password);
                return;
            }

            String sep = "/";
            String workRemoteDir = StrUtils.joinEmpty(remoteDir, sep, oppositePath);
            // 
            try {
                int n = workRemoteDir.indexOf("/", 1);
                String subRemoteDir = null;
                while (n != -1) {
                    subRemoteDir = workRemoteDir.substring(0, n);
                    if (!ftpClient.changeWorkingDirectory(subRemoteDir)) {
                        ftpClient.makeDirectory(subRemoteDir);
                    }
                    n = workRemoteDir.indexOf("/", n + 1);
                }
                ftpClient.makeDirectory(workRemoteDir);
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);

            conf.setServerLanguageCode("zh");

            // 
            ftpClient.changeWorkingDirectory(workRemoteDir);

            ftpClient.setBufferSize(1024);

            ftpClient.setControlEncoding("GBK");

            // 
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

            // 
            boolean state = ftpClient.storeFile(uploadFileName, is);

            log.debug("storeFile state = " + state);

            ftpClient.logout();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();

            }
        }

    }

    /**
     * 
     * 
     * @param oppositePath 
     * @param downFileName  ??
     * @return
     */
    public static InputStream delete(String oppositePath, String fileName) {
        InputStream is = null;
        try {
            // // 
            ftpClient.connect(host, port);

            // 
            ftpClient.login(user, password);

            // ??
            int reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                log.error("FTP server refused connection,host=" + host + ",user=" + user + "," + password);
                return is;
            }

            if (fileName != null) {
                String sep = "/";

                String deleteFileName = StrUtils.joinEmpty(remoteDir, sep, oppositePath, sep, fileName);

                ftpClient.deleteFile(deleteFileName);

            }
            ftpClient.logout();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                ftpClient.disconnect();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        return is;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String filePath = "E:/test/test.txt";
        File file = new File(filePath);
        InputStream is = null;
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        upload("test/abc/", "test.txt", is);

    }

}