FTP工具类

来源:互联网 发布:epoll 高性能网络库 编辑:程序博客网 时间:2024/06/06 03:49
import java.io.IOException;import java.io.InputStream; import org.apache.commons.net.ftp.FTP;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPReply;/** * FTP工具类 * * @author 13074110 */public class FtpUtil {       /**     * @param url     *            远程机器IP     * @param port     *            ftp 端口 21     * @param username     *            用户名     * @param password     *            密码     * @param path     *            服务器上的文件路径     * @param filename     *            上传服务器上的文件名     * @param input     *            输入流(把上传的文件转为输入流)     * @return     * @throws IOException     * @throws SocketException     */    public static String uploadFile(String ip, int port ,String username,            String password, String path,String definePath, String filename,             InputStream input)throws IOException    {         String[] defines = null;        FTPClient ftp = null;        if(ftp == null)        {            ftp = createConnection( ip, port, username,password);        }        if(!ftp.isConnected())        {            ftp.connect(ip);            ftp.login(username, password);            int reply = ftp.getReplyCode();            if (!FTPReply.isPositiveCompletion(reply))             {                ftp.disconnect();                return null;            }        }        //ftp.enterLocalPassiveMode();           // 转到指定上传目录                defines = definePath.split("/");                 boolean ishave = ftp.changeWorkingDirectory(path+definePath);         if(ishave){            ftp.setFileType(FTP.BINARY_FILE_TYPE);            // 将上传文件存储到指定目录            ftp.storeFile(filename, input);        }else{            boolean createsuccess = false;//ftp.makeDirectory(path.substring(0,path.lastIndexOf("/")));                        String tempPath = path;            for(int i = 0 ; i < defines.length ; i ++){                tempPath = tempPath.concat(defines[i]).concat("/");                if(ftp.changeWorkingDirectory(tempPath)){                    continue;                }else{                    createsuccess = ftp.makeDirectory(tempPath);                                    }                //如果当前子目录创建失败,不继续创建下层子目录                if(!createsuccess){                    break;                }            }                        if(createsuccess){                ftp.changeWorkingDirectory(path+definePath);                ftp.setFileType(FTP.BINARY_FILE_TYPE);                // 将上传文件存储到指定目录                ftp.storeFile(filename, input);            }                        }        //  关闭输入流        input.close();        closeConnection(ftp);        return path+filename;    }        public static FTPClient createConnection(String ip, int port,             String username,String password) throws IOException    {        // 创建FTPClient对象        FTPClient ftp = new FTPClient();        int reply;        // 连接FTP服务器        // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器        ftp.connect(ip);        // 登录ftp        ftp.login(username, password);        // 看返回的值是不是230,如果是,表示登录成功        reply = ftp.getReplyCode();        // 以2开头的返回值就会为真        if (!FTPReply.isPositiveCompletion(reply))         {            ftp.disconnect();            return null;        }        return ftp;    }        public static void closeConnection(FTPClient ftp) throws IOException    {        // 退出ftp        ftp.logout();        if (ftp.isConnected())        {            ftp.disconnect();        }    }}