Ftp文件的上传和下载(二)

 public class DataTransfer {
 static final Logger log = Logger.getLogger(DataTransfer.class);
 //定义出公共参数定义表ComParas的全局对象
    static ComParasManager comParasManager = (ComParasManager)JpaUtil.getFactary().getBean("comParasManager");
 
    private static StaticUserAuthenticator auth = null;
    private static FileSystemOptions opts = null;
    //取出ftp服务器的IP地址和账号信息
    private static String serverHost = (String)comParasManager.getComParasByKey("quickViewFTPServer");
    private static String username = (String)comParasManager.getComParasByKey("quickViewFTPUserName");;
    private static String password = (String)comParasManager.getComParasByKey("quickViewFTPPassWord");
    public static String USER_NAME = "userName";
    public static String USER_PASSWORD = "password";
    public static String FTP_ADDRESS = "address";
    private static String FTP_FILEADDRESS = "fileaddress";
    private static FTPClient ftpClient = null;
    public static String FTP_FILE = "ftp:";
    private static String FTP_SEPARATOR = "/";
     
    
    public static void main(String[] args) {
  
     boolean flag = transferFilesToFtp("C:\\1.txt", "ftp://用户名:密码@IP/space/ftp/sate/1.txt", false, null);
     System.out.println("推送成功的标志位:"+flag);
 }
    /***
     * ftpClient 适用于ftp://用户名:密码@ftp服务器的IP地址/文件存放的相对地址(不包含主路径)/文件名称
     * vfs bug,从ftp服务器上某文件指向的本地地址向ftp传送文件的问题
     * @param sourceFile 源文件全路径
     * @param remoteFile  文件在FTP上的最终路径 格式:ftp://用户名:密码@ftp服务器的IP地址/文件存放的相对地址(不包含主路径)/文件名称
     * @param isNeedDel 是否需要删除源文件
     */
    public static boolean transferFileToFtp(String sourceFile, String relativePath, boolean isNeedDel) {
     
     String remoteFile = "ftp://"+username+":"+password+"@"+serverHost+relativePath;
     log.info("要推送的ftp服务器地址为:"+remoteFile);
        //可以创建多级文件夹
        return transferFilesToFtp(sourceFile, remoteFile, isNeedDel, null);
    }
    
    public static void setUserAuthenticator(String domain, String username, String password) throws FileSystemException {
        try {
            opts = new FileSystemOptions();
            auth = new StaticUserAuthenticator(domain, username, password);
            DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
            FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
        } catch (FileSystemException ex) {
            JpaUtil.handleException("FTP权限认证{}", ex);
        }
    }
    /**
     * 建立FTP连接
     */
    public static boolean connectFtpServer() throws Exception {
        boolean theConnection = false;
        if (ftpClient == null) {
            ftpClient = new FTPClient();
            ftpClient.connect(DataTransfer.serverHost);
            ftpClient.login(DataTransfer.username, DataTransfer.password);
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                log.error("FTP server refused connection");
                ftpClient.disconnect();
            } else {
                theConnection = true;
            }
        } else {
            theConnection = true;
        }
        return theConnection;
    }
    /***
     * 获取FTP连接
     * @param ftpClient
     */
    public static FTPClient getFtpConnection(String remoteFile) {
        String sourceDir = getFileDir(remoteFile);
        Properties prop = getFtpInfo(sourceDir);
        sourceDir = prop.getProperty(FTP_FILEADDRESS);
        String ftpUserName = prop.getProperty(USER_NAME);
        String ftpPassword = prop.getProperty(USER_PASSWORD);
        String ftpServerHost = prop.getProperty(FTP_ADDRESS);
        return getFTPClientConnection(ftpServerHost, ftpUserName, ftpPassword);
    }
    
    /**
     * 获取FTP链接 :FTPClient
     * @param ipAdress ip地址
     * @param userName  用户名
     * @param userPwd  密码
     * @return FTPClient
     */
    public static FTPClient getFTPClientConnection(String ipAdress, String userName, String userPwd) {
        FTPClient currentFtpClient = new FTPClient();
        try {
            currentFtpClient.setDataTimeout(600000);//设置数据传输超时时间10分钟
            currentFtpClient.setConnectTimeout(60000);// 设置连接超时时间为1分钟
            try {
                currentFtpClient.connect(ipAdress);
            } catch (IOException e) {
                try {
                    if (currentFtpClient != null && currentFtpClient.isConnected()) {
                        currentFtpClient.disconnect();
                    }
                } catch (Exception exx) {
                    log.error("getFTPClientConnection exception is:{}"+exx.getMessage());
                }
                currentFtpClient = null;
                log.error("getFTPClientConnection exception =={}"+e.getMessage());
                return currentFtpClient;
            }
            currentFtpClient.login(userName, userPwd);
            //currentFtpClient.enterLocalPassiveMode();
            currentFtpClient.setFileType(FTP.BINARY_FILE_TYPE);
            int replyCode = currentFtpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                
                log.error("FTP server refused connection");
                currentFtpClient.disconnect();
                return null;
            }
        } catch (Exception ex) {
            log.error("getFTPClientConnection:exception =={}", ex);
        }
        return currentFtpClient;
    }
    /***
     * 关闭FTP连接
     * @param ftpClient
     */
    public static void closeFtpConnection(FTPClient currentFtpClient) {
        try {
            if (currentFtpClient != null && currentFtpClient.isConnected()) {
                currentFtpClient.logout();
                currentFtpClient.disconnect();
            }
        } catch (IOException ex) {
            try {
                if (currentFtpClient != null && currentFtpClient.isConnected()) {
                    currentFtpClient.disconnect();
                }
            } catch (Exception e) {
                JpaUtil.handleException("close ftpClient connection failed{}", e);
            }
        } finally {
            currentFtpClient = null;
        }
    }
    /***
     * 关闭FTP连接
     * @param ftpClient
     */
    @Deprecated
    public static void closeFtpConnection() {
        try {
            if (ftpClient != null && ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            JpaUtil.handleException("close ftpClient connection failed{}", ex);
        } finally {
            ftpClient = null;
        }
    }
    /**
     * 不可以创建文件夹
     * 可接收外部传入的ftp链接,若外部传入的ftp链接为null,则程序从地址解析并获得链接
     * @param sourceFile
     * @param remoteFile
     * @param isNeedDel
     * @param commonFtpClient
     * @return
     */
    public static boolean transferFileToFtp(String sourceFile, String remoteFile, boolean isNeedDel, FTPClient commonFtpClient) {
      
     log.info("sourceFile:"+sourceFile);
     log.info("remoteFile:"+remoteFile);
     log.info("isNeedDel:"+isNeedDel); 
        boolean theResult = false;
        //若是外部传入的ftp链接则此段程序不关闭链接 ,主要用来根据推送目的地分批推送,而不用建立每个文件都建立新链接
        boolean isNeedCloseCrrentFtpClient = true; //是否关闭当前链接
        BufferedInputStream buffIn = null;
        String targetDir = null;
        File tempFile = null;
        String resultDir = "";
        String fileName = "";
        FTPClient currentFtpClient = null;//commonFtpClient;//
        try {
            //检查文件是否存在
            tempFile = new File(sourceFile);
            log.info("tempFile:"+tempFile);
            if (tempFile.exists()) {
                tempFile.length();
                log.info("临时文件的长度为:"+tempFile.length());
            } else {
                log.error("sourefile do not exits");
                return false;
            }
            fileName = getFileName(sourceFile);
            targetDir = getFileDir(remoteFile);
            
            log.info("fileName:"+fileName + " === " +"targetDir:"+targetDir);
            //通过推送地址获取相关属性 ,如解析为程序能识别的地址 
            Properties prop = getFtpInfo(targetDir);
            log.info("prop:"+prop);
            targetDir = prop.getProperty(FTP_FILEADDRESS);
            log.info("targetDir:"+targetDir);
            //判断传入的ftp链接是否已经关闭或者是否是null
            if (commonFtpClient == null || !(commonFtpClient.isConnected())) {//从 prop获取ftp信息并根据信息获取链接
                String ftpUserName = prop.getProperty(USER_NAME);
                String ftpPassword = prop.getProperty(USER_PASSWORD);
                String ftpServerHost = prop.getProperty(FTP_ADDRESS);
                currentFtpClient = getFTPClientConnection(ftpServerHost, ftpUserName, ftpPassword);
                log.info("ftpUserName:"+ftpUserName);
                log.info("ftpPassword:"+ftpPassword);
                log.info("ftpServerHost:"+ftpServerHost);
                log.info("currentFtpClient:"+currentFtpClient);
            } else {
             //应用传入的ftp链接
                currentFtpClient = commonFtpClient;
                isNeedCloseCrrentFtpClient = false;
            }
            if (!targetDir.equals("")) {
             //确定resultDir
                resultDir = targetDir + FTP_SEPARATOR;
                log.info("resultDir:"+resultDir);
            }
            
            if (currentFtpClient != null && currentFtpClient.isConnected()) {//若已链接
                /**
                 * 文件夹不存在创建文件夹
                 */
                boolean s = currentFtpClient.changeWorkingDirectory(targetDir);
                
                log.info("targetDir:"+targetDir+"是否创建成功文件夹:"+s);
                if ((!targetDir.equals("")) && !s) {
                    currentFtpClient.makeDirectory(targetDir);
                    currentFtpClient.changeWorkingDirectory(targetDir);
                }

                buffIn = new BufferedInputStream(new FileInputStream(tempFile));
                log.info("resultDir+fileName:" + resultDir + fileName);
                log.info("tempFile:" + tempFile.getPath());
                theResult = currentFtpClient.storeFile(fileName, buffIn);
                log.info("=======" + theResult);
                //检查传送后文件的正确性
                if (theResult) {
                    //删除源文件
                    if (isNeedDel) {
                     //需删除源文件
                        theResult = tempFile.delete();
                    }   
                } else {//传送失败
                    return false;
                }
            } else {
                log.error("transferFileToFTP: 登录FTP服务器失败");
            }
        } catch (Exception ex) {
            JpaUtil.handleException("==transferFileToFtp: {}", ex);
            String logContent = "transferFileToFtp: " + ex.getMessage();
            log.error(logContent);       
            theResult = false;
        } finally {//关闭文件流操作
            try {
                if (buffIn != null) {
                    IOUtils.closeQuietly(buffIn);
                    buffIn = null;
                }
            } catch (Exception ex) {
                log.error("close buffin failed,{}", ex);
            }
            if (isNeedCloseCrrentFtpClient) {
                closeFtpConnection(currentFtpClient);
            }
        }
        return theResult;
    }
    /**
     * 可以创建多级文件夹
     * 可接收外部传入的ftp链接,若外部传入的ftp链接为null,则程序从地址解析并获得链接
     * @param sourceFile
     * @param remoteFile
     * @param isNeedDel
     * @param commonFtpClient
     * @return
     */
    public static boolean transferFilesToFtp(String sourceFile, String remoteFile, boolean isNeedDel, FTPClient commonFtpClient) {
        boolean theResult = false;
        //若是外部传入的ftp链接则此段程序不关闭链接 ,主要用来根据推送目的地分批推送,而不用建立每个文件都建立新链接
        boolean isNeedCloseCrrentFtpClient = true; //是否关闭当前链接
        BufferedInputStream buffIn = null;
        String targetDir = null;
        File tempFile = null;
        String resultDir = "";
        String fileName = "";
        FTPClient currentFtpClient = null;//commonFtpClient;//
        try {
            //检查文件是否存在
            tempFile = new File(sourceFile);
            if (tempFile.exists()) {
                tempFile.length();
            } else {
                log.error("sourefile do not exits");
                return false;
            }
            fileName = getFileName(sourceFile);
            targetDir = getFileDir(remoteFile);
            //通过推送地址获取相关属性 ,如解析为程序能识别的地址 
            Properties prop = getFtpInfo(targetDir);
            targetDir = prop.getProperty(FTP_FILEADDRESS);
            //判断传入的ftp链接是否已经关闭或者是否是null
            if (commonFtpClient == null || !(commonFtpClient.isConnected())) {//从 prop获取ftp信息并根据信息获取链接
                String ftpUserName = prop.getProperty(USER_NAME);
                String ftpPassword = prop.getProperty(USER_PASSWORD);
                String ftpServerHost = prop.getProperty(FTP_ADDRESS);
                currentFtpClient = getFTPClientConnection(ftpServerHost, ftpUserName, ftpPassword);
            } else {//应用传入的ftp链接
                currentFtpClient = commonFtpClient;
                isNeedCloseCrrentFtpClient = false;
            }
            if (!targetDir.equals("")) {//确定resultDir
                resultDir = targetDir + FTP_SEPARATOR;
                log.info("resultDir:"+resultDir);
            }
            if (currentFtpClient != null && currentFtpClient.isConnected()) {//若已链接
                /**
                 * 文件夹不存在创建文件夹
                 */
              System.out.println("FTP_SEPARATOR:"+FTP_SEPARATOR);
                currentFtpClient.changeWorkingDirectory(FTP_SEPARATOR);
                boolean s = currentFtpClient.changeWorkingDirectory(targetDir);
                if ((!targetDir.equals("")) && !s) {
                    String[] dirs = targetDir.split(FTP_SEPARATOR);
                    currentFtpClient.changeWorkingDirectory(FTP_SEPARATOR);
                    for (int i = 0; i < dirs.length; i++) {
                        String dir = dirs[i];
                        currentFtpClient.makeDirectory(dir);
                        s = currentFtpClient.changeWorkingDirectory(dir);
                    }
                    if (!s) {
                        return s;
                    }
                }
                buffIn = new BufferedInputStream(new FileInputStream(tempFile));
                theResult = currentFtpClient.storeFile(fileName, buffIn);
                //检查传送后文件的正确性
            } else {
                log.error("transferFileToFTP: 登录FTP服务器失败");
            }
        } catch (Exception ex) {
            JpaUtil.handleException("==transferFileToFtp: {}", ex);
            log.error(ex.toString());
            theResult = false;
        } finally {//关闭文件流操作
            try {
                if (buffIn != null) {
                    IOUtils.closeQuietly(buffIn);
                    buffIn = null;
                    if (theResult) {
                        //通过ftpclient获得的文件大小和通过File对象获得的文件大小不一致
                        //删除源文件
                        if (isNeedDel) {
                            //需删除源文件
                            theResult = tempFile.delete();
                        }
                    } else {//传送失败
                        return false;
                    }
                }
            } catch (Exception ex) {
                log.error("close buffin failed,{}", ex);
            }
            if (isNeedCloseCrrentFtpClient) {
                closeFtpConnection(currentFtpClient);
            }
        }
        return theResult;
    }
    
    public static boolean transferImagesToFtp(String sourceFile, byte[] image, String remoteFile, FTPClient commonFtpClient) {
        boolean theResult = false;
        boolean isNeedCloseCrrentFtpClient = true;
        BufferedInputStream buffIn = null;
        String targetDir = null;
        String resultDir = "";
        String fileName = "";
        FTPClient currentFtpClient = null;
        try {
            if (image == null) {
                log.error("image is null in database");
                return false;
            }
            fileName = getFileName(sourceFile);
            targetDir = getFileDir(remoteFile);
            Properties prop = getFtpInfo(targetDir);
            targetDir = prop.getProperty(FTP_FILEADDRESS);
            if ((commonFtpClient == null) || (!commonFtpClient.isConnected())) {
                String ftpUserName = prop.getProperty(USER_NAME);
                String ftpPassword = prop.getProperty(USER_PASSWORD);
                String ftpServerHost = prop.getProperty(FTP_ADDRESS);
                currentFtpClient = getFTPClientConnection(ftpServerHost, ftpUserName, ftpPassword);
            } else {
                currentFtpClient = commonFtpClient;
                isNeedCloseCrrentFtpClient = false;
            }
            if (currentFtpClient == null) {
                return false;
            }
            if (!targetDir.equals("")) {
                resultDir = targetDir + FTP_SEPARATOR;
            }
            if ((currentFtpClient != null) && (currentFtpClient.isConnected())) {
                if(!targetDir.startsWith(FTP_SEPARATOR))
                targetDir = FTP_SEPARATOR + targetDir;
                boolean s = currentFtpClient.changeWorkingDirectory(targetDir);
                if ((!targetDir.equals("")) && (!s)) {
                    String[] dirs = targetDir.split(FTP_SEPARATOR);
                    for (int i = 0; i < dirs.length; i++) {
                        String dir = dirs[i];
                        if (!"".equals(dir)) {
                            currentFtpClient.makeDirectory(dir);
                            s = currentFtpClient.changeWorkingDirectory(dir);
                        }
                    }
                    if (!s) {
                    return s;
                    }
                }
                buffIn = new BufferedInputStream(new ByteArrayInputStream(image));
                log.info("resultDir+fileName:" + resultDir + fileName);
                theResult = currentFtpClient.storeFile(fileName + ".JPG", buffIn);
                log.info("=======" + theResult);
            } else {
                String logContent = "transferFileToFtp:登录FTP服务器失败";
                log.info(logContent);
            }
        } catch (Exception ex) {
            JpaUtil.handleException("==transferFileToFtp: {}", ex);
            String logContent = "transferFileToFtp: " + ex.getMessage();
            log.error(logContent);
            theResult = false;
        } finally {
            try {
                if (buffIn != null) {
                    IOUtils.closeQuietly(buffIn);
                    buffIn = null;
                }
                if (!isNeedCloseCrrentFtpClient) {
                    currentFtpClient.changeWorkingDirectory(FTP_SEPARATOR);
                }
            } catch (Exception ex) {
                log.error("close buffin failed,{}", ex);
            }
            if (isNeedCloseCrrentFtpClient) {
                closeFtpConnection(currentFtpClient);
            }
        }
        return theResult;
    }

    /**
     * 获取文件名
     * @param tempName
     * @return
     */
    public static String getFileName(String tempName) {
        String fileName = null;
        int theLastPlace1 = tempName.lastIndexOf("\\");
        int theLastPlace2 = tempName.lastIndexOf("/");
        int theStartPlace = theLastPlace1 > theLastPlace2 ? theLastPlace1 : theLastPlace2;
        fileName = tempName.substring(theStartPlace + 1);
        return fileName;
    }
    /**
     * 获取文件路径
     * @param tempName
     * @return
     */
    public static String getFileDir(String tempName) {
        String fileDir = null;
        int theLastPlace1 = tempName.lastIndexOf("\\");
        int theLastPlace2 = tempName.lastIndexOf("/");
        int theStart = theLastPlace1 > theLastPlace2 ? theLastPlace1 : theLastPlace2;
        fileDir = tempName.substring(0, theStart);
        return fileDir;
    }
    //格式== ftp://ars2usr2:1234abcd@10.24.252.4/VIRRL1QUICKVIEWFTPLINK/20080810
    public static Properties getFtpInfo(String ftpFilePath) {
     
     log.info("ftpFilePath:"+ftpFilePath);
        Properties prop = new Properties();
        String userName = null;
        String userPassword = null;
        String ftpAddress = null;
        String fileAddress = "";
        String[] buffer;
        buffer = ftpFilePath.split("/");
        if (buffer.length < 3) {
            return null;
        }
        for (int i = 3; i < buffer.length; i++) {
            if (i == 3) {
                fileAddress += buffer[i];
            } else {
                fileAddress += FTP_SEPARATOR + buffer[i];
            }
        }
        buffer = buffer[2].split(":");
        if (buffer.length != 2) {
            return null;
        }
        userName = buffer[0];
        buffer = buffer[1].split("@");
        if (buffer.length != 2) {
            return null;
        }
        userPassword = buffer[0];
        ftpAddress = buffer[1];
        log.info("=" + userName + "=" + userPassword + "=" + ftpAddress + "=" + fileAddress);
        prop.put(USER_NAME, userName);
        log.info("getFtpInfo:userName:" + userName);
        prop.put(USER_PASSWORD, userPassword);
        prop.put(FTP_ADDRESS, ftpAddress);
        prop.put(FTP_FILEADDRESS, fileAddress);
        return prop;
    }
}

转载于:https://my.oschina.net/bigdataer/blog/424761

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值