Java实现FTP上传下载功能
Java实现FTP上传下载功能
Java FTP客户端工具包很多,在此我选用的Apache的FTPClient。这个包的获取可以通过[url]http://commons.apache.org/net/[/url]来获取,我使用的是最新的commons-net-1.4.1.zip。其中包含了众多的java网络编程的工具包,官方文档列举如下:
支持网络协议如下:
FTP
NNTP
SMTP
POP3
Telnet
TFTP
Finger
Whois
rexec/rcmd/rlogin
Time (rdate) and Daytime
Echo
Discard
NTP/SNTP
支持网络协议如下:
FTP
NNTP
SMTP
POP3
Telnet
TFTP
Finger
Whois
rexec/rcmd/rlogin
Time (rdate) and Daytime
Echo
Discard
NTP/SNTP
都很有用。在此我用到的是FTP相关的一些包。
在编写程序之前,先配置一个简单的FTP服务器,如下图:
下面是一个使用FTPClient来实现的FTP上传和下载的功能,主要是对此包有个感性的认识。例程如下:
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileOutputStream;
/**
* Apache commons-net 试用一把,看看FTP客户端工具做的好用不
*
* @author : leizhimin,2008-8-20 14:00:38。<p>
*/
public class FtpTest {
public static void main(String[] args) {
testUpload();
testDownload();
}
/**
* FTP上传单个文件测试
*/
public static void testUpload() {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
try {
ftpClient.connect("192.168.14.117");
ftpClient.login("admin", "123");
File srcFile = new File("C:\\new.gif");
fis = new FileInputStream(srcFile);
//设置上传目录
ftpClient.changeWorkingDirectory("/admin/pic");
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
//设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile("3.gif", fis);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
IOUtils.closeQuietly(fis);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
}
/**
* FTP下载单个文件测试
*/
public static void testDownload() {
FTPClient ftpClient = new FTPClient();
FileOutputStream fos = null;
try {
ftpClient.connect("192.168.14.117");
ftpClient.login("admin", "123");
String remoteFileName = "/admin/pic/3.gif";
fos = new FileOutputStream("c:/down.gif");
ftpClient.setBufferSize(1024);
//设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.retrieveFile(remoteFileName, fos);
} catch (IOException e) {
e.printStackTrace();
throw neaw RuntimeException("FTP客户端出错!", e);
} finally {
IOUtils.closeQuietly(fos);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
}
}
import org.apache.commons.net.ftp.FTPClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileOutputStream;
/**
* Apache commons-net 试用一把,看看FTP客户端工具做的好用不
*
* @author : leizhimin,2008-8-20 14:00:38。<p>
*/
public class FtpTest {
public static void main(String[] args) {
testUpload();
testDownload();
}
/**
* FTP上传单个文件测试
*/
public static void testUpload() {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
try {
ftpClient.connect("192.168.14.117");
ftpClient.login("admin", "123");
File srcFile = new File("C:\\new.gif");
fis = new FileInputStream(srcFile);
//设置上传目录
ftpClient.changeWorkingDirectory("/admin/pic");
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
//设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile("3.gif", fis);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
IOUtils.closeQuietly(fis);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
}
/**
* FTP下载单个文件测试
*/
public static void testDownload() {
FTPClient ftpClient = new FTPClient();
FileOutputStream fos = null;
try {
ftpClient.connect("192.168.14.117");
ftpClient.login("admin", "123");
String remoteFileName = "/admin/pic/3.gif";
fos = new FileOutputStream("c:/down.gif");
ftpClient.setBufferSize(1024);
//设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.retrieveFile(remoteFileName, fos);
} catch (IOException e) {
e.printStackTrace();
throw neaw RuntimeException("FTP客户端出错!", e);
} finally {
IOUtils.closeQuietly(fos);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
}
}
测试结果表明,文件上传、下载均成功!
感觉Apache做的不错,就用这个了。
相关推荐
hickwu 2020-10-23
杨立峰 2020-08-18
diediexiaomi 2020-08-11
wq0 2020-07-18
leodengzx 2020-06-28
liyansring 2020-06-21
jiahaohappy 2020-06-21
leodengzx 2020-06-14
xsg 2020-06-09
piaotiejun 2020-06-07
TangCuYu 2020-06-05
clmngu 2020-06-03
河的第三条岸 2020-05-31
atb 2020-05-30
书弋江山 2020-05-30
LWLWLiang 2020-05-28
JJandYY 2020-05-25
饮马天涯 2020-05-20
wennuanwarm 2020-05-16