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

    package xxxx;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    
    import org.apache.commons.net.ftp.FTP;
    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPFile;
    import org.apache.commons.net.ftp.FTPReply;
    
    public class FtpUtil {
    
        private final static String host = "192.168.xxx.xxxx";
        
        private final static String basePath = "/auth";
        
        private final static String username = "";
        
        private final static String password = "";
        
        public static boolean uploadFile(String localPath) throws IOException {
            File file = new File(localPath);
            FileInputStream fileInputStream = null;
            boolean result = false;
            FTPClient ftp = new FTPClient();
            try {
                //----------------------
                //尝试添加超时时间 解决阻塞问题
                ftp.setDefaultTimeout(60 * 1000);
                ftp.setConnectTimeout(60 * 1000);
                ftp.setDataTimeout(60 * 1000);
                //----------------------
                fileInputStream = new FileInputStream(file);
                String fileName = file.getName();
                int reply;
                ftp.connect(host);// 连接FTP服务器
                ftp.login(username, password);// 登录
                reply = ftp.getReplyCode();
                //230代表登录成功
                System.out.println(reply);
                if (!FTPReply.isPositiveCompletion(reply)) {
                    ftp.disconnect();
                    return result;
                }
                ftp.changeWorkingDirectory(basePath);
                ftp.setFileType(FTP.BINARY_FILE_TYPE);
                //ftp.setBufferSize(1024);
                ftp.setControlEncoding("GBK");
                //设置被动模式
                ftp.enterLocalPassiveMode();
                //设置上传文件的类型为二进制类型
                //上传文件
                
                result = ftp.storeFile(new String(fileName.getBytes("GBK"), "iso-8859-1"), fileInputStream);
                fileInputStream.close();
                ftp.logout();
    //            result = true;
            } finally {
                if (ftp.isConnected()) {
                    try {
                        ftp.disconnect();
                    } catch (IOException ioe) {
                        
                    }
                }
            }
            return result;
            
        }
        
        
        
        public static boolean uploadFile(File file) throws IOException {
            FileInputStream fileInputStream = null;
            boolean result = false;
            FTPClient ftp = new FTPClient();
            try {
                //----------------------
                //尝试添加超时时间 解决阻塞问题
                ftp.setDefaultTimeout(60 * 1000);
                ftp.setConnectTimeout(60 * 1000);
                ftp.setDataTimeout(60 * 1000);
                //----------------------
                fileInputStream = new FileInputStream(file);
                String fileName = file.getName();
                int reply;
                ftp.connect(host);// 连接FTP服务器
                ftp.login(username, password);// 登录
                reply = ftp.getReplyCode();
                //230代表登录成功
                System.out.println(reply);
                if (!FTPReply.isPositiveCompletion(reply)) {
                    ftp.disconnect();
                    return result;
                }
                ftp.changeWorkingDirectory(basePath);
                ftp.setFileType(FTP.BINARY_FILE_TYPE);
                //ftp.setBufferSize(1024);
                ftp.setControlEncoding("GBK");
                //设置被动模式
                ftp.enterLocalPassiveMode();
                //设置上传文件的类型为二进制类型
                //上传文件
                
                result = ftp.storeFile(new String(fileName.getBytes("GBK"), "iso-8859-1"), fileInputStream);
                fileInputStream.close();
                ftp.logout();
    //            result = true;
            } finally {
                if (ftp.isConnected()) {
                    try {
                        ftp.disconnect();
                    } catch (IOException ioe) {
                        
                    }
                }
            }
            return result;
            
        }
        
        public static boolean downloadFile(String remotePath, String fileName, String localPath) {
            boolean result = false;
            FTPClient ftp = new FTPClient();
            try {
                int reply;
                ftp.connect(host);
                // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
                ftp.login(username, password);// 登录
                reply = ftp.getReplyCode();
                System.out.println(reply);
                if (!FTPReply.isPositiveCompletion(reply)) {
                    ftp.disconnect();
                    return result;
                }
                ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
                ftp.enterLocalPassiveMode();
                FTPFile[] fs = ftp.listFiles();
                for (FTPFile ff : fs) {
                    if (ff.getName().equals(fileName)) {
                        File localFile = new File(localPath + "/" + ff.getName());
    
                        OutputStream is = new FileOutputStream(localFile);
                        ftp.retrieveFile(ff.getName(), is);
                        is.close();
                    }
                }
    
                ftp.logout();
                result = true;
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (ftp.isConnected()) {
                    try {
                        ftp.disconnect();
                    } catch (IOException ioe) {
                    }
                }
            }
            return result;
        }
    
        
        /*public static void main(String[] args) {
            String localpath = "D:/ftp测试.txt";
            System.out.println(uploadFile(localpath));
            
            System.out.println(downloadFile("/auth", "ftp测试.txt", "D:/log"));
        }*/
    }

    使用的jar包

    <dependency>
        <groupId>commons-net</groupId>
        <artifactId>commons-net</artifactId>
        <version>2.0</version>
    </dependency>
  • 相关阅读:
    [Java解惑]数值表达式
    Java使用LdAP获取AD域用户
    LDAP Error Codes
    Excel向上取整
    java中的三种取整函数
    Dwz手册的补充说明和常见问题
    【转】BSON数据格式
    go语言合并两个数组
    vscode远程修改文件('file': A system error occured )
    [转]Linux 桌面玩家指南:20. 把 Linux 系统装入 U 盘打包带走
  • 原文地址:https://www.cnblogs.com/kongxianghao/p/8607445.html
Copyright © 2011-2022 走看看