zoukankan      html  css  js  c++  java
  • FTPUtil工具类

    配置maven

     <dependency>
                <groupId>commons-net</groupId>
                <artifactId>commons-net</artifactId>
                <version>3.6</version>
      </dependency>
    

    工具类

    package com.hk.utils;
    
    
    import org.apache.commons.net.ftp.FTP;
    import org.apache.commons.net.ftp.FTPClient;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    
    /**
     * User: hk
     * Date: 2017/8/10 下午4:31
     * version: 1.0
     */
    public class FTPUtil implements AutoCloseable {
    
        private FTPClient ftpClient;
    
    
        public FTPUtil(String serverIP, int port, String userName, String password) throws IOException {
            ftpClient = new FTPClient();
            ftpClient.connect(serverIP, port);
            ftpClient.login(userName, password);
            ftpClient.setBufferSize(1024);//设置上传缓存大小
            ftpClient.setControlEncoding("UTF-8");//设置编码
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//设置文件类型
        }
    
        /**
         * 下载ftp文件到本地
         *
         * @param remoteFileName 远程文件名称
         * @param localFile      本地文件[包含路径]
         * @return true/false
         * @throws IOException 异常
         */
        public boolean downloadFile(String remoteFileName, String localFile) throws IOException {
            boolean isSucc;
            File outFileName = new File(localFile);
            if (ftpClient == null)
                throw new IOException("ftp server not login");
            try (OutputStream outputStream = new FileOutputStream(outFileName)) {
                isSucc = ftpClient.retrieveFile(remoteFileName, outputStream);
            }
            return isSucc;
        }
    
        /**
         * 上传文件制定目录
         *
         * @param remoteFileName 远程文件名
         * @param localFile      本地文件[必须带路径]
         * @return true/false
         * @throws IOException 异常
         */
        public boolean uploadFile(String remoteFileName, String localFile) throws IOException {
            boolean isSucc;
            try (InputStream inputStream = new FileInputStream(localFile)) {
                if (ftpClient == null)
                    throw new IOException("ftp server not login");
                isSucc = ftpClient.storeFile(remoteFileName, inputStream);
            }
            return isSucc;
        }
    
        /**
         * 切换目录
         *
         * @param path 创建目录
         * @return 创建标志
         * @throws IOException 异常
         */
        public boolean changeDirectory(String path) throws IOException {
            return ftpClient.changeWorkingDirectory(path);
        }
    
        /**
         * 创建目录
         *
         * @param path 创建目录
         * @return 创建标志
         * @throws IOException 异常
         */
        public boolean createDirectory(String path) throws IOException {
            return ftpClient.makeDirectory(path);
        }
    
    
        /**
         * 自动关闭资源
         */
        @Override
        public void close() throws Exception {
            if (ftpClient != null && ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        }
    }
    
    

    使用方式

    @Test
        public void downloadFile() throws Exception {
            try (FTPUtil ftpUtil = new FTPUtil("10.211.55.7",21,"ftpuser","123qwe")){
                ftpUtil.downloadFile("2.txt","/Users/hk/Desktop/22.txt");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    
        @Test
        public void uploadFile() throws Exception {
            try (FTPUtil ftpUtil = new FTPUtil("10.211.55.6",21,"ftpuser","123qwe")){
                ftpUtil.uploadFile("2.txt","/Users/hk/Desktop/副本.txt");
            }catch (Exception e){
                e.printStackTrace();
            }
    
    
        }
    
  • 相关阅读:
    Java 泛型 泛型的约束与局限性
    Java 泛型 泛型方法
    Java 泛型 泛型数组
    Java 泛型 协变性、逆变性
    Java 泛型 协变式覆盖和泛型重载
    Java 泛型 泛型代码和虚拟机
    Insertion Sort List
    Remove Duplicates from Sorted List II
    String to Integer (atoi)
    SpringMvc源码入门
  • 原文地址:https://www.cnblogs.com/muyl/p/7348698.html
Copyright © 2011-2022 走看看