使用的是apache开源包commons-net-3.3.jar所提供的FTPClient

FTP服务器使用Quick Easy FTP Server 4.0.0(服务器ip为192.168.31.104,端口使用默认21端口,用户名为test,密码为123)

JDK版本为1.6,Junit使用4.8.1

FTP上传工具类:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient; public class FTPutils {
public static FTPClient getFTPClient(String ip, int port, String uName,
String uPwd) {
FTPClient ftpClient = new FTPClient();
boolean result = true;
try {
// use port 21 by default
// ftpClient.connect(ip);
// use specific port
ftpClient.connect(ip, port);
if (ftpClient.isConnected()) {
boolean flag = ftpClient.login(uName, uPwd);
if (flag) {
ftpClient.setControlEncoding("GBK");
// binary file
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
} else {
result = false;
}
} else {
result = false;
}
if (result) {
return ftpClient;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
} public static void close(InputStream in, OutputStream out,
FTPClient ftpClient) {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Input stream close error!");
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Onput stream close error!");
}
}
if (null != ftpClient) {
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Ftp client stream close error!");
}
}
} public static boolean testUpload(String ip, int port, String uName,
String uPwd, String fileName, String localPath, String remotePath) {
boolean result = true;
FileInputStream in = null;
FTPClient ftpClient = getFTPClient(ip, port, uName, uPwd);
if (null == ftpClient) {
System.out.println("Get FTP client failure!");
return false;
}
try {
File file = new File(localPath + fileName);
in = new FileInputStream(file); ftpClient.changeWorkingDirectory(remotePath);
ftpClient.storeFile(fileName, in); return result;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
close(in, null, ftpClient);
}
} public static boolean testDownload(String ip, int port, String uName,
String uPwd, String fileName, String localPath, String remotePath) {
boolean result = true;
FileOutputStream out = null;
FTPClient ftpClient = getFTPClient(ip, port, uName, uPwd);
if (null == ftpClient) {
System.out.println("Get FTP client failure!");
return false;
}
try {
File file = new File(localPath + fileName);
out = new FileOutputStream(file); ftpClient.changeWorkingDirectory(remotePath);
ftpClient.retrieveFile(fileName, out);
return result;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
close(null, out, ftpClient);
}
}
}

Junit测试类:

test1:上传本地e盘中up.txt文件至FTP服务器根目录下upfile文件夹中

test2:将FTP服务器根目录下downfile文件夹中的down.txt文件下载至本地e盘中

import static org.junit.Assert.*;
import org.junit.Test; public class FTPutilsTest {
@Test
public void testTestUpload() {
boolean result = FTPutils.testUpload("192.168.31.104", 21, "test", "123",
"up.txt", "e:\\", "/upfile");
assertTrue(result == true);
} @Test
public void testTestDownload() {
boolean result = FTPutils.testDownload("192.168.31.104", 21, "test", "123",
"down.txt", "e:\\", "/downfile");
assertTrue(result == true);
}
}

FTP服务器日志为:

04/11/2013 17:50:34.833 (000004)	 - (not logged in)	(192.168.31.104)>	220 Welcome to LZL's FTP Server V4.0.0
04/11/2013 17:50:34.835 (000004) - (not logged in) (192.168.31.104)> USER test
04/11/2013 17:50:34.836 (000004) - (not logged in) (192.168.31.104)> 331 Password required for test
04/11/2013 17:50:34.837 (000004) - (not logged in) (192.168.31.104)> PASS 123
04/11/2013 17:50:34.839 (000004) - test (192.168.31.104)> 230 Client :test successfully logged in. Client IP :192.168.31.104
04/11/2013 17:50:34.840 (000004) - test (192.168.31.104)> TYPE I
04/11/2013 17:50:34.841 (000004) - test (192.168.31.104)> 200 Type set to I
04/11/2013 17:50:34.843 (000004) - test (192.168.31.104)> CWD /upfile
04/11/2013 17:50:34.843 (000004) - test (192.168.31.104)> 250 "/upfile" is current directory.
04/11/2013 17:50:34.847 (000004) - test (192.168.31.104)> PORT 192,168,31,104,255,56
04/11/2013 17:50:34.852 (000004) - test (192.168.31.104)> 200 Port command successful.
04/11/2013 17:50:34.853 (000004) - test (192.168.31.104)> STOR up.txt
04/11/2013 17:50:34.895 (000004) - test (192.168.31.104)> 150 Opening BINARY mode data connection for file transfer.
04/11/2013 17:50:34.902 (000004) - test (192.168.31.104)> 226 Transfer complete.
04/11/2013 17:50:34.903 (000004) - test (192.168.31.104)> QUIT
04/11/2013 17:50:34.904 (000004) - test (192.168.31.104)> 220 Bye
04/11/2013 17:50:34.912 (000004) - test (192.168.31.104)> Client :test disconnected from 192.168.31.104
04/11/2013 17:50:34.920 (000005) - (not logged in) (192.168.31.104)> 220 Welcome to LZL's FTP Server V4.0.0
04/11/2013 17:50:34.921 (000005) - (not logged in) (192.168.31.104)> USER test
04/11/2013 17:50:34.922 (000005) - (not logged in) (192.168.31.104)> 331 Password required for test
04/11/2013 17:50:34.922 (000005) - (not logged in) (192.168.31.104)> PASS 123
04/11/2013 17:50:34.932 (000005) - test (192.168.31.104)> 230 Client :test successfully logged in. Client IP :192.168.31.104
04/11/2013 17:50:34.948 (000005) - test (192.168.31.104)> TYPE I
04/11/2013 17:50:34.958 (000005) - test (192.168.31.104)> 200 Type set to I
04/11/2013 17:50:34.974 (000005) - test (192.168.31.104)> CWD /downfile
04/11/2013 17:50:34.984 (000005) - test (192.168.31.104)> 250 "/downfile" is current directory.
04/11/2013 17:50:34.994 (000005) - test (192.168.31.104)> PORT 192,168,31,104,255,59
04/11/2013 17:50:35.008 (000005) - test (192.168.31.104)> 200 Port command successful.
04/11/2013 17:50:35.016 (000005) - test (192.168.31.104)> RETR down.txt
04/11/2013 17:50:35.026 (000005) - test (192.168.31.104)> 150 Opening BINARY mode data connection for file transfer.
04/11/2013 17:50:35.041 (000005) - test (192.168.31.104)> 226 Transfer complete.
04/11/2013 17:50:35.050 (000005) - test (192.168.31.104)> QUIT
04/11/2013 17:50:35.090 (000005) - test (192.168.31.104)> 220 Bye
04/11/2013 17:50:35.116 (000005) - test (192.168.31.104)> Client :test disconnected from 192.168.31.104

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

  1. JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)

    package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...

  2. windows系统下ftp上传下载和一些常用命令

    先假设一个ftp地址 用户名 密码 FTP Server: home4u.at.china.com User: yepanghuang Password: abc123 打开windows的开始菜单, ...

  3. windows下ftp上传下载和一些常用命令

    先假设一个ftp地址 用户名 密码 FTP Server: home4u.at.china.com User: yepanghuang Password: abc123 打开windows的开始菜单, ...

  4. FTP上传下载工具(FlashFXP) v5.5.0 中文版

    软件名称: FTP上传下载工具(FlashFXP) 软件语言: 简体中文 授权方式: 免费试用 运行环境: Win 32位/64位 软件大小: 7.4MB 图片预览: 软件简介: FlashFXP 是 ...

  5. 高可用的Spring FTP上传下载工具类(已解决上传过程常见问题)

    前言 最近在项目中需要和ftp服务器进行交互,在网上找了一下关于ftp上传下载的工具类,大致有两种. 第一种是单例模式的类. 第二种是另外定义一个Service,直接通过Service来实现ftp的上 ...

  6. C# -- FTP上传下载

    C# -- FTP上传下载 1. C#实现FTP下载 private static void TestFtpDownloadFile(string strFtpPath, string strFile ...

  7. Java.ftp上传下载

    1:jar的maven的引用: 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...

  8. python之实现ftp上传下载代码(含错误处理)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之实现ftp上传下载代码(含错误处理) #http://www.cnblogs.com/kait ...

  9. python之模块ftplib(实现ftp上传下载代码)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块ftplib(实现ftp上传下载代码) #需求:实现ftp上传下载代码(不含错误处理) f ...

  10. java客户端调用ftp上传下载文件

    1:java客户端上传,下载文件. package com.li.utils; import java.io.File; import java.io.FileInputStream; import ...

随机推荐

  1. BZOJ_1221_ [HNOI2001]_软件开发(最小费用流,网络流24题#10)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1221 n天,每天需要r个毛巾,用完以后可以洗,要么花fa洗a天,要么花fb洗b天,毛巾不够了可 ...

  2. 【转】java中静态代码块的用法 static用法详解

    原文网址:http://www.cnblogs.com/panjun-Donet/archive/2010/08/10/1796209.html (一)java 静态代码块 静态方法区别一般情况下,如 ...

  3. Oracle 12c创建用户时出现“ORA-65096: invalid common user or role name”的错误

    这篇文章主要介绍CDB和PDB的基本管理,资料来源oracle官方. 基本概念: Multitenant Environment:多租户环境 CDB(Container Database):数据库容器 ...

  4. python __enter__ 与 __exit__的作用,以及与 with 语句的关系

    转载自:http://linbo.github.io/2013/01/08/python-with/ (一直不知道博客园哪里发转载文章) With语句是什么? 有一些任务,可能事先需要设置,事后做清理 ...

  5. C# Dynamic特性

    C# 4.0 dynamic:声明动态对象 dynamic关键字用于声明一个动态对象,然后通过该动态对象去调用方法或读写属性.这是C#4.0 添加的特性.官方解释:dynamic类型是帮助我们绕过编译 ...

  6. 一个free的问题

    请看下面的代码: #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sy ...

  7. Xmind Pro 3.4.0.201311050558 Xmind 3.4 破解版 Crack

    其实就一个附件.某大神那里的下不到了.从这里就好了. 使用方法请参见压缩包~ 如果连接不能用了请及时告知回复.>< 仅适用于与版本号为201311050558的Xmind.当然尊重正版开发 ...

  8. hadoop入门必备基础知识

    1.对Linux 系统的要求        会基本的命令:        (1)知道root用户        (2)ls命令会查看文件夹内容        (3)cd命令等2.Java 的要求    ...

  9. Codeforces2B - The least round way(DP)

    题目大意 给定一个N*N的格子,每个格子里有一个非负数,要求你找出从左上角到右下角的一条路径,使得它满足路径上的格子里的数全部乘起来的积尾部0最少 题解 如果要产生0肯定是2*5得出来的,最终的乘积可 ...

  10. centos svn快速搭建

    搭建SVN服务,有效的管理代码,以下三步可以快速搞定. 1.安装 #yum install subversion 判断是否安装成功 [root@]# svnserve --version 有了SVN软 ...