zoukankan      html  css  js  c++  java
  • FTP 上传资源

        /**
         * 把资源文件上传到CDN
         */
        public static boolean ftpUpload(String ip,String username,String password,String fileName,int port,String srcPath){
            FTPClient ftpClient = new FTPClient();
            FileInputStream inputStream = null;
            boolean sussess = false;
            try {
                ftpClient.connect(ip,port);
                ftpClient.login(username, password);
                
                ftpClient.enterLocalPassiveMode();
                //ftpClient.enterLocalActiveMode();
                //ftpClient.enterRemotePassiveMode();
               ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); 
                
                
                File srcFile = new File(fileName);
                inputStream = new FileInputStream(srcFile);
                
                //设置上传目录
                ftpClient.makeDirectory(srcPath);
                ftpClient.changeWorkingDirectory(srcPath);
                ftpClient.setBufferSize(1024*1024*10);
                ftpClient.setControlEncoding("GBK");
                
                
                //设置文件类型
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                sussess = ftpClient.storeFile(fileName.substring(fileName.lastIndexOf("/")+1), inputStream);
                System.out.println("ftpClient.getStatus()"+ftpClient.getStatus());
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("FTP客户端出错!", e); 
            }finally{
                try {
                    inputStream.close();
                    ftpClient.logout();
                } catch (IOException e) {
                    e.printStackTrace();
                     throw new RuntimeException("关闭FTP连接发生异常!", e); 
                }
                
            }
            return sussess;
        }
    /**
         * 把资源文件上传到CDN
         */
        public static boolean uploadSrcFileToCdn(String ip,String username,String password,String fileName,int port,String srcPath){
            FTPClient ftpClient = new FTPClient();
            FileInputStream inputStream = null;
            boolean sussess = false;
            try {
                ftpClient.connect(ip,port);
                ftpClient.login(username, password);
                
                ftpClient.enterLocalPassiveMode();
                //ftpClient.enterLocalActiveMode();
                //ftpClient.enterRemotePassiveMode();
                ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); 
                
                
                File srcFile = new File(fileName);
                inputStream = new FileInputStream(srcFile);
                
                //设置上传目录
                ftpClient.makeDirectory(srcPath);
                ftpClient.changeWorkingDirectory(srcPath);
                ftpClient.setBufferSize(1024*1024*10);
                ftpClient.setControlEncoding("GBK");
                
                
                //设置文件类型
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                sussess = ftpClient.storeFile(fileName.substring(fileName.lastIndexOf("/")+1), inputStream);
                System.out.println("ftpClient.getStatus()"+ftpClient.getStatus());
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("FTP客户端出错!", e); 
            }finally{
                try {
                    inputStream.close();
                    ftpClient.logout();
                } catch (IOException e) {
                    e.printStackTrace();
                     throw new RuntimeException("关闭FTP连接发生异常!", e); 
                }
                
            }
            return sussess;
        }
  • 相关阅读:
    负载均衡获得真实源IP的6种方法
    美图全链路监控实战
    移动端APM网络监控与优化方案
    k8s 如何对外提供服务
    mysql5.7安装audit审计插件
    mysql 5.7安装密码校验插件validate_password
    Linux Crontab 定时任务
    stm32 hard fault usage fault UNALIGNED -> task stack overflow
    linux逻辑卷管理(LVM)
    suse11开启telnet服务
  • 原文地址:https://www.cnblogs.com/phyxis/p/5256781.html
Copyright © 2011-2022 走看看