1、需要引入外部jar包:commons-net-2.0.jar(或者是子包commons-net-ftp-2.0.jar)

2、需下载ftp服务器

3、 本地电脑访问ftp服务器格式:ftp://用户名:密码@ftp服务器ip

4、以下用api访问

STEP1:

Java代码
/**  
 * 连接并登录FTP   
 * @param hostname:ftp地址(不带ftp://)   即ip
 * @param username:登录用户名  
 * @param password:登录密码  
 **/  
public int openFtp(String hostname, String username, String password) {   
    int result = 0;   
       
    // 第一步:实例化FTPClient   
    ftpClient = new FTPClient();   
       
    try {   
        // 第二步:连接FTP   
        ftpClient.connect(hostname);   
           
        // 第三步:登录FTP   
        ftpClient.login(username, password);   
           
    } catch (SocketException e) {   
        // 连接错误时捕捉此异常   
        result = 1;   
    } catch (IOException e) {   
        e.printStackTrace();   
    }    
       
    return result;   
}

STEP2:

Java代码
/**  
 * 下载单个文件  
 *   
 * @param remoteFile  
 *            :需要下载的文件,格式为ftp://xx.xx.xx.xx/remoteFile,如:ftp://10.10.10.10/dir1  
 *            /dir2/file.txt,则remoteFile为dir1/dir2/file.txt  
 * @param localFile:下载的文件保存到本地的文件,为完整绝对路径。  
 * @return  
 */  
public int ftpDownload(String remoteFile, String localFile) {   
    FileOutputStream fos = null;   
    InputStream is = null;   
    try {   
        // 第一步:设置基本属性   
        ftpClient.setBufferSize(1024);   
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);   
           
        // 第二步:获取远程文件的输入流   
        is = ftpClient.retrieveFileStream(remoteFile);   
           
        if (is == null) {   
            // 如果输入流为空,则表示要下载的远程文件不存在   
            return 0;   
        } else {   
            // 如果输入流不为空,则将远程文件的输入流写到本地   
            fos = new FileOutputStream(localFile);   
            byte[] buffer = new byte[1024];   
            int i = -1;   
            while ((i = is.read(buffer)) != -1) {   
                fos.write(buffer, 0, i);   
            }   
        }   
  
    } catch (FileNotFoundException e) {   
        e.printStackTrace();   
    } catch (IOException e) {   
        e.printStackTrace();   
    } finally {   
        // 关闭输入输出流   
        IOUtils.closeQuietly(is);   
        IOUtils.closeQuietly(fos);   
    }   
  
    return 1;   
}

STEP3:

STEP4:

Java代码
/**  
 * 退出登录ftp,并断开连接  
 */  
public void closeFtp() {   
    try {   
        if (ftpClient != null) {   
            ftpClient.logout();   
            ftpClient.disconnect();   
        }   
    } catch (IOException e) {   
        // 断开连接错误时捕捉此异常   
    }   
}

原文来自:雨枫技术教程网 http://www.fengfly.com
原文网址:http://www.fengfly.com/plus/view-190551-1.html

FTP进行上传下载文件的更多相关文章

  1. linux下常用FTP命令 上传下载文件【转】

    1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...

  2. Linux 终端访问 FTP 及 上传下载 文件

    今天同事问我一个问题,在Linux 下访问FTP,并将文件上传上去. 我之前一直是用WinSCP工具的. 先将文件从linux copy到windows下,然后在传到ftp上.google 一下. 方 ...

  3. shell通过ftp实现上传/下载文件

    直接代码,shell文件名为testFtptool.sh: #!/bin/bash ########################################################## ...

  4. Linux 终端訪问 FTP 及 上传下载 文件

    今天同事问我一个问题,在Linux 下訪问FTP,并将文件上传上去. 我之前一直是用WinSCP工具的. 先将文件从linux copy到windows下,然后在传到ftp上. google 一下. ...

  5. Linux 终端访问 FTP 及 上传下载 文件[转]

    1.      Linux 终端连接FTP [oracle@Dave ~]$ ftp 10.85.7.97 Connected to 10.85.7.97. 220 Serv-U FTP Server ...

  6. 使用批处理文件在FTP服务器 上传下载文件

    1.从ftp服务器根目录文件夹下的文件到指定的文件夹下 格式:ftp -s:[配置文件] [ftp地址] 如:ftp -s:c:\vc\ftpconfig.txt   192.168.1.1 建立一个 ...

  7. Spring学习---Spring中利用组件实现从FTP服务器上传/下载文件

    FtpUtil.java import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcepti ...

  8. HCNA管理设置文件系统FTP服务上传下载文件

    1.拓扑图 2.R2配置 The device is running! ###################################### <Huawei>sys Enter s ...

  9. 使用ftp软件上传下载php文件时换行丢失bug

    正 文:   在使用ftp软件上传下载php源文件时,我们偶尔会发现在本地windows下notepad++编辑器写好的php文件,在使用ftp上传到linux服务器后,php文件的换行符全部丢失了, ...

随机推荐

  1. Sherlock and Squares

    //Link https://www.hackerrank.com/challenges/sherlock-and-squares from math import sqrt # 用什么,引什么,减少 ...

  2. MySQL表复制

    http://www.2cto.com/database/201202/120259.html http://www.cnblogs.com/sunss/archive/2010/10/08/1845 ...

  3. cf C. Quiz

    http://codeforces.com/contest/337/problem/C 得到的分数为:(2^1+2^2+...+2^X)*k + m-X*k = (2^(X+1)-2)*k + m-X ...

  4. CRC校验源码分析

    这两天做项目,需要用到 CRC 校验.以前没搞过这东东,以为挺简单的.结果看看别人提供的汇编源程序,居然看不懂.花了两天时间研究了一下 CRC 校验,希望我写的这点东西能够帮助和我有同样困惑的朋友节省 ...

  5. 【转】 Ubuntu下配置USB转串口及串口工具配置--不错

    原文网址:http://blog.csdn.net/dreambegin/article/details/6985028 注意:默认情况下ubuntu已经安装了USB转串口驱动(pl2303).我的是 ...

  6. Windows下AndroidStudio 中使用Git(AndroidStudio项目于GitHub关联)

    前提条件 : 1. 安装 Git 客户端 下载链接 2. 有 GitHub 账号 (假设你已经有了一些git基础, 如果还一点都不会, 请去找其他加成学习) AndroidStudio项目发布到Git ...

  7. 查看登录用户who

    几个命令:wwho每隔5秒钟,就来查看hadoop是否已经登录,如登录,显示其已经登录,并退出:sleep whoami last,显示/var/log/wtmp文件,显示用户登录历史及系统重启历史  ...

  8. 04747_Java语言程序设计(一)_第4章_数组和字符串

    面试题 字符串连接 public class Aserver { public static void main(String args[]) { // 字符串数据和其他数据+,结果是字符串类型 // ...

  9. python高级编程之最佳实践,描述符与属性01

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #最佳实践 """ 为了避免前面所有的 ...

  10. 在iOS上present一个半透明的viewController

    UIViewController *viewController = [[UIViewController alloc]init]; UIViewController* controller = se ...