FTP工具类

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();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值