使用的是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. sdut2536字母哥站队(dp)

    简单DP  说是简单 还是推了好一会 推出来觉得好简单 保留当前i的最小值 dp[i] = min(dp[i],dp[j]+i-j-1) j<i #include <iostream> ...

  2. hdu4671Backup Plan

    http://acm.hdu.edu.cn/showproblem.php?pid=4671 这个高端的题意啊 看了N久啊 n>m时  直接第一列按顺序来 第二列为M+1 else  第一列顺序 ...

  3. Innodb加载数据字典 && flush tables

    测试了两个case,属于之前blog的遗留问题: innodb如何加载数据字典 flush tables都做了什么操作 先来看下innodb加载数据字典: 首次使用:select * from tt; ...

  4. bzoj1797

    其实我觉得这种题目风格很像今天省选第三轮D1T1 都是在一个算法模型上去探索规律: 首先我们要做一遍最大流毫无疑问 第一问看起来很好想,只要是满流边就可以了? 错,反例不难找到 如:1--->2 ...

  5. MySQL配置文件-my.ini

    下面允许我介绍一下MySQL的my.ini配置文件: my.ini是什么? my.ini是MySQL数据库中使用的配置文件,修改这个文件可以达到更新配置的目的. my.ini存放在哪里? my.ini ...

  6. C#常用的命名规范

    C#常用的命名规则 Pascal 规则 每个单词开头的字母大写(如 TestCounter). Camel 规则 除了第一个单词外的其他单词的开头字母大写. 如. testCounter. Upper ...

  7. LightOJ 1259 Goldbach`s Conjecture 水题

    不想说了 #include <cstdio> #include <iostream> #include <ctime> #include <vector> ...

  8. IIS7 性能(内存、CPU、当前请求耗时)监测

    程序上线了,但运行过程中如果发现很CPU.内存异常,某些操作耗时,如何在生产环境中监测并查明原因呢,有以下几种工具(方式): 1.NProfiler,这是一个商业软件,有试用周期,可以监测包括Winf ...

  9. C++之内部类(嵌套类)与外部类及友元

    本人能力.精力有限,所言所感都基于自身的实践和有限的阅读.查阅,如有错误,欢迎拍砖,敬请赐教——博客园:钱智慧. 先上代码: class Outer { public: Outer(){m_outer ...

  10. nyoj 96 一个水题目

    虽然很简单,发现自己用内存太高了了,算了,我就是这水平了. 描述 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则求出w的后n-1位的数. 输入 第一行为M,表示 ...