FTP上传文件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;

public class FTPTest
{
    public FTPClient ftpClient = null;
    private String serverIP = "";
    private String serverName = "";
    private String password = "";
    
    Logger loger = org.apache.log4j.Logger.getLogger(FTPTest.class);
    
    public FTPTest(String serverIP,String serverName,String password)
    {
        this.serverIP = serverIP;
        this.serverName = serverName;
        this.password = password;
    }
    
    public boolean getConnection()
    {
        boolean canConnection = false;
        boolean isConnectionSuccess = false;
        
        try 
        {
            ftpClient = new FTPClient();
            ftpClient.setConnectTimeout(3600000);   //连接超时为60分钟
            ftpClient.setDataTimeout(3600000);
            
            ftpClient.connect(serverIP);
            
//          loger.info(traceStr+"Connected to " + serverIP + ".");
            
            int reply = ftpClient.getReplyCode();
            
            canConnection = FTPReply.isPositiveCompletion(reply);  //可以判断是否可以连接
//          loger.info(""+"是否可以连接:"+canConnection);
            
            if(canConnection)
            {
                isConnectionSuccess = ftpClient.login(serverName, password);
                if(!isConnectionSuccess)
                {
                    loger.info("FTP服务器连接错误,请重新配置!");
                }
                else
                {
                    loger.info("FTP连接服务器成功......");
                }
                    
            }
            
        } catch (SocketException e) {
            e.printStackTrace();
            
        } catch (IOException e) {
            e.printStackTrace();
            
        } 
        
        return isConnectionSuccess;
    }
    
    /**
     * @param clientFilePath  本地文件路径
     * @param serverPath  需要上传到服务器的文件路径
     * @return  上传成功返回为true,失败则为false
     */
    public boolean upLoadFileToServer(String clientFilePath,String serverPath)
    {
        boolean isUpLoadSuccess = false;
        
        //如果在windows下,文件的路径为"\"方式,所以ftp到linux下要将路径转换一下
        String serverParentPath = (new File(serverPath).getParent()).replace("\\", "/");
        
        
        File file = new File(clientFilePath);
        loger.info("上传文件之前大小:"+file.length()/1024.);
        
        FileInputStream input = null; 
        try {
            //上传文件之前需要设置文件属性
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            
            //当可以切换到服务器端的目录时,则认为有此目录,否则认为没有此目录需要创建
            boolean isServerPathExist = ftpClient.changeWorkingDirectory(serverParentPath);
            loger.info("服务器端文件 "+serverParentPath+" 是否存在:"+isServerPathExist);
            if(!isServerPathExist)
            {
                boolean isMkdirSucdess = ftpClient.makeDirectory(serverParentPath);
                loger.info("创建目录是否成功:"+isMkdirSucdess);
            }
            
            input = new FileInputStream(clientFilePath);
            ftpClient.enterLocalPassiveMode();
            //此处开始上传文件
            isUpLoadSuccess = ftpClient.storeFile(serverPath, input); 
            
            loger.info("上传文件是否成功:"+isUpLoadSuccess);
            
        } catch (FileNotFoundException e) {
            loger.error(e);
            
        } catch (IOException e) {
            loger.error(e);
            
        }
        finally
        {
            if(null!=input)
            {
                try {
                    input.close();
                } catch (IOException e) {
                    loger.error(e);
                    
                }
            }
        }
        
        return isUpLoadSuccess;
    
    }
    
    public void disConnection()
    {
        if(null!=ftpClient)
        {
            try
            {
                ftpClient.logout();  //登出
                ftpClient.disconnect();  //断开连接
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args)
    {
        //程序执行前,加载日志配置文件
        PropertyConfigurator.configure(System.getProperty("user.dir") + "\\config\\log4jConfig.properties") ;
        
        String serverIP = "serverIP";
        String userName = "userName";
        String password = "password";
        String clientFilePath = "clientFilePath";
        String serverPath = "serverPath";
        FTPTest test = new FTPTest(serverIP,userName,password);
        test.getConnection(); //获取连接
        test.upLoadFileToServer(clientFilePath, serverPath);  //上传文件
        test.disConnection();  //断开连接
        
    }
}

 在此我用到了commons-net-2.0.jar包用于FTP,log4j-1.2.16.jar用来记录日志

FTPS也是同样的操作,就是初始化的时候需要传入一个连接方式:ftpsClient = new FTPSClient("SSL");

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值