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;
        }
  • 相关阅读:
    【vue坑】vue组件不显示,没加载dom
    vue radio绑定数据
    git修改密码遇到的坑 git -- Authentication failed for
    python全局变量
    adb无法使用,提示error: unknown host service的解决办法
    uiautomator2 init初始化失败
    【解决方案】安装lxml失败 Installing lxml
    【一般都是源的问题】ubuntu使用apt-get update更新失败
    ubuntu 安装python3.6 以及安装pip3 出现Command '('lsb_release', '-a')' returned non-zero exit status 1问题解决
    ubuntu设置python软链python3.5和python3.6同时存在,python3指令使用python3.6
  • 原文地址:https://www.cnblogs.com/phyxis/p/5256781.html
Copyright © 2011-2022 走看看