FTP上传下载

时间:2023-02-25 15:28:58

使用的是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上传下载代码&lpar;含错误处理&rpar;

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

  9. python之模块ftplib&lpar;实现ftp上传下载代码&rpar;

    # -*- 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. linux下普通用户如何使用80端口启动程序

    linux下普通用户如何使用80端口启动程序 http://blog.csdn.net/shootyou/article/details/6750230 大家都知道默认情况下linux的1024以下端 ...

  2. linux服务器报Too many open files的解决方法

    linux 上tomcat 服务器抛出socket异常“文件打开太多”的问题 java.net.SocketException: Too many open filesat java.net.Plai ...

  3. WebSocket技术

    webSocket技术 在html5技术革新中,加入了WebSocket技术 1.webSocket实际是TCP连接 webSocket在最初将发送http连接请求到服务器端, 但是在header中加 ...

  4. Web前端新人笔记之CSS值和单位

    数字 颜色——命名颜色 在Css2.1中规范定义了17个颜色名.包括html4.0中定义的16个颜色及外加一个橙色: <h1 style="color=aqua">aq ...

  5. Ajax跨域请求——PHP服务端处理

    header('Access-Control-Allow-Origin:*'); // 响应类型 header('Access-Control-Allow-Methods:POST'); // 响应头 ...

  6. Webpack飞行手册

    前言 在学习 Webpack 之前,我们需要了解一个概念:模块. 何为模块? 如果你曾学过 Java , C# 之类的语言,一定会知道 Java 中的 import 或 C# 中的 using 吧? ...

  7. 为什么Java不能以返回值区分重载方法?

    读者可能会想:"在区分重载方法的时候,为什么只能以类名和方法的形参列表作为标准呢?能否考虑用方法的返回值来区分呢?" 比如下面两个方法,虽然他们有相同的名字和形式参数,但却很容易区 ...

  8. tcp没用吗?为什么MOBA、&OpenCurlyDoubleQuote;吃鸡”游戏不推荐用tcp协议

    本文由云+社区发表 作者:腾讯云游戏行业资深架构师 余国良 MOBA类和"吃鸡"游戏为什么对网络延迟要求高? 我们知道,不同类型的游戏因为玩法.竞技程度不一样,采用的同步算法不一样 ...

  9. Merge Sort&lpar;Java&rpar;

    public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextI ...

  10. MariaDB与MySQL

    一.MariaDB安装部署 tar zxvf mariadb-5.5.31-linux-x86_64.tar.gz mv mariadb-5.5.31-linux-x86_64 /usr/local/ ...