FTP附件传输工具类-FtpUtil

package com.test;

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

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTPClient;


/**
 * ftp附件传输工具类
 * @since jdk1.6
 *
 */

public class FtpUtil {

    public String ftpRootPath = "";
    public String ftpIp = "";
    public int ftpPort = 21;
    public String ftpUser = "";
    public String ftpPassword = "";
    
    /**
     * ftp附件传输方法,使用前需将FtpUtil的变量(ftpRootPath、ftpIp、ftpPort、ftpUser、ftpPassword)赋值
     * 1.ftpRootPath: ftp根目录;
     * 2.ftpIp: ftp IP地址;
     * 3.ftpPort: ftp端口号;
     * 4.ftpUser: 用户名;
     * 5.ftpPassword: 密码;
     * @param srcPath 附件源路径
     * @param path 上传相对路径
     * @return true:上传成功; false:上传失败
     */
    public boolean upload(String srcPath,String path){
        String attName = srcPath.substring(srcPath.lastIndexOf("/")+1);
        System.out.println("-----transfer start-----:" + attName);
        boolean flag = false;
        FTPClient ftpClient = new FTPClient();
        FileInputStream fis = null;
        try { 
            ftpClient.connect(ftpIp,ftpPort); 
            ftpClient.login(ftpUser, ftpPassword);
            File srcFile = new File(srcPath);
            fis = new FileInputStream(srcFile); 
            ftpClient.changeWorkingDirectory(ftpRootPath);
            //设置上传目录
            if(!change2TargetDirectory(ftpClient,path)){
                return false;
            }
            ftpClient.setBufferSize(1024); 
            ftpClient.setControlEncoding("UTF-8");
            //ftpClient.enterLocalPassiveMode();
            //设置文件类型(二进制) 
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            flag = ftpClient.storeFile(attName, fis);
            if(flag){
                System.out.println("-----transfer result-----:success" + attName);
            }else{
                System.out.println("-----transfer result-----:fail" + attName);
            }
            fis.close();
            ftpClient.logout();
        } catch (IOException e) { 
            e.printStackTrace(); 
            throw new RuntimeException("FTP客户端出错!", e); 
        } finally { 
            IOUtils.closeQuietly(fis); 
            try { 
                ftpClient.disconnect(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
                throw new RuntimeException("关闭FTP连接发生异常!", e); 
            } 
        }
        return flag;
    }
    
    private boolean change2TargetDirectory(FTPClient ftpClient,String path){
        boolean result = true; 
        try {
            //路径为空则转到根目录
            if(StringUtils.isEmpty(path)){
                ftpClient.changeWorkingDirectory(ftpRootPath);
            }
            String[] pathArr = path.split("/");
            for(int i=0;i<pathArr.length;i++){
                if(ftpClient.changeWorkingDirectory(pathArr[i])){
                    continue;
                }else{
                    if(!ftpClient.makeDirectory(pathArr[i]) || !ftpClient.changeWorkingDirectory(pathArr[i])){
                        result = false;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("切换FTP目录出错!", e);
        }
        return result;
    }
    
}


你可能感兴趣的:(FTP附件传输工具类-FtpUtil)