FTP服务器文件操作 二:文件下载(测试可用)

版权声明:本文为博主原创文章,未经博主允许不得复制发布,转载没问题的!盗内容你丫就是我儿子。 https://blog.csdn.net/u010785811/article/details/54582694

之前有个喜人的家伙问我,说为啥我不管,我就帮他远程了以下问题,发现他的地址密码全是乱输的~~~~你们别逗我好不好

虽然我不经常玩这个东西,可。。哎

算了帖代码

/**
         * Description: 从FTP服务器下载文件
         *
         * @param url
         *            FTP服务器hostname
         * @param port
         *            FTP服务器端口
         * @param username
         *            FTP登录账号
         * @param password
         *            FTP登录密码
         * @param remotePath
         *            FTP服务器上的相对路径
         * @param fileName
         *            要下载的文件名
         * @param localPath
         *            下载后保存到本地的路径
         * @return
         */
        public static boolean downFile(String url, int port, String username,
                                       String password, String remotePath, String fileName,
                                       String localPath) {
            // 初始表示下载失败
            boolean success = false;
            FTPClient ftp = null;
            String proxySet = System.getProperty("proxySet");
            //true表示有设置代理
            // 创建FTPClient对象
            if("true".equals(proxySet)) {
                ftp = new FTPHTTPClient(System.getProperty("http.proxyHost"), Integer.parseInt(System.getProperty("http.proxyPort")));
            } else {
                ftp = new FTPClient();
            }

            try {
                int reply;
                // 连接FTP服务器
                // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
                ftp.connect(url, port);
                // 登录ftp
                ftp.login(username, password);
                reply = ftp.getReplyCode();
                if (!FTPReply.isPositiveCompletion(reply)) {
                    ftp.disconnect();
                    return success;
                } // 转到指定下载目录
                ftp.changeWorkingDirectory(remotePath);
                //如果有配置代理参数时,需要设置如下模式
                if("true".equals(proxySet)) {
                    ftp.enterLocalPassiveMode();
                }
                ftp.enterLocalPassiveMode();
                // 列出该目录下所有文件
                FTPFile[] fs = ftp.listFiles();
                // 遍历所有文件,找到指定的文件
                for (FTPFile ff : fs) {
                    if (ff.getName().equals(fileName)) {
                        File dir = new File(localPath);
                        if (!dir.exists()) {
                            if (!dir.mkdirs()) {
                                throw new IOException("创建保存目录失败");
                            }
                        }

                        // 根据绝对路径初始化文件
                        File localFile = new File(localPath + "/" + ff.getName());
                        // 输出流
                        OutputStream is = new FileOutputStream(localFile);
                        // 下载文件
                        ftp.retrieveFile(ff.getName(), is);
                        is.close();
                    }
                }
                // 退出ftp
                ftp.logout();
                // 下载成功
                success = true;
            } catch (IOException e) {
//          e.printStackTrace();
                System.out.println(e);
            } finally {
                if (ftp.isConnected()) {
                    try {
                        ftp.disconnect();
                    } catch (IOException ioe) {
                    }
                }
            }
            return success;
        }
文件上传操作:http://blog.csdn.net/denghejing/article/details/41650271

猜你喜欢

转载自blog.csdn.net/u010785811/article/details/54582694
今日推荐