当前位置: 移动技术网 > IT编程>开发语言>Java > JAVA中使用FTPClient实现文件上传下载实例代码

JAVA中使用FTPClient实现文件上传下载实例代码

2019年07月22日  | 移动技术网IT编程  | 我要评论

在java程序开发中,ftp用的比较多,经常打交道,比如说向ftp服务器上传文件、下载文件,本文给大家介绍如何利用jakarta commons中的ftpclient(在commons-net包中)实现上传下载文件。

一、上传文件

原理就不介绍了,大家直接看代码吧

/** 
* description: 向ftp服务器上传文件 
* @version1.0 jul 27, 2008 4:31:09 pm by 崔红保(cuihongbao@d-heaven.com)创建 
* @param url ftp服务器hostname 
* @param port ftp服务器端口 
* @param username ftp登录账号 
* @param password ftp登录密码 
* @param path ftp服务器保存目录 
* @param filename 上传到ftp服务器上的文件名 
* @param input 输入流 
* @return 成功返回true,否则返回false 
*/ 
publicstaticboolean uploadfile(string url,int port,string username, string password, string path, string filename, inputstream input) { 
boolean success = false; 
ftpclient ftp = new ftpclient(); 
try { 
int reply; 
ftp.connect(url, port);//连接ftp服务器 
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接ftp服务器 
ftp.login(username, password);//登录 
reply = ftp.getreplycode(); 
if (!ftpreply.ispositivecompletion(reply)) { 
ftp.disconnect(); 
return success; 
} 
ftp.changeworkingdirectory(path); 
ftp.storefile(filename, input); 

input.close(); 
ftp.logout(); 
success = true; 
} catch (ioexception e) { 
e.printstacktrace(); 
} finally { 
if (ftp.isconnected()) { 
try { 
ftp.disconnect(); 
} catch (ioexception ioe) { 
} 
} 
} 
return success; 
}<pre></pre> 
/**
* description: 向ftp服务器上传文件
* @version1.0 jul 27, 2008 4:31:09 pm by 崔红保(cuihongbao@d-heaven.com)创建
* @param url ftp服务器hostname
* @param port ftp服务器端口
* @param username ftp登录账号
* @param password ftp登录密码
* @param path ftp服务器保存目录
* @param filename 上传到ftp服务器上的文件名
* @param input 输入流
* @return 成功返回true,否则返回false
*/
public static boolean uploadfile(string url,int port,string username, string password, string path, string filename, inputstream input) {
boolean success = false;
ftpclient ftp = new ftpclient();
try {
int reply;
ftp.connect(url, port);//连接ftp服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接ftp服务器
ftp.login(username, password);//登录
reply = ftp.getreplycode();
if (!ftpreply.ispositivecompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeworkingdirectory(path);
ftp.storefile(filename, input); 

input.close();
ftp.logout();
success = true;
} catch (ioexception e) {
e.printstacktrace();
} finally {
if (ftp.isconnected()) {
try {
ftp.disconnect();
} catch (ioexception ioe) {
}
}
}
return success;
} 

下面我们写两个小例子:

1.将本地文件上传到ftp服务器上,代码如下:

@test 
publicvoid testuploadfromdisk(){ 
try { 
fileinputstream in=new fileinputstream(new file("d:/test.txt")); 
boolean flag = uploadfile("127.0.0.1", 21, "test", "test", "d:/ftp", "test.txt", in); 
system.out.println(flag); 
} catch (filenotfoundexception e) { 
e.printstacktrace(); 
} 
}<pre></pre> 
@test
public void testuploadfromdisk(){
try {
fileinputstream in=new fileinputstream(new file("d:/test.txt"));
boolean flag = uploadfile("127.0.0.1", 21, "test", "test", "d:/ftp", "test.txt", in);
system.out.println(flag);
} catch (filenotfoundexception e) {
e.printstacktrace();
}
} 

2.在ftp服务器上生成一个文件,并将一个字符串写入到该文件中

@test 
publicvoid testuploadfromstring(){ 
try { 
inputstream input = new bytearrayinputstream("test ftp".getbytes("utf-8")); 
boolean flag = uploadfile("127.0.0.1", 21, "test", "test", "d:/ftp", "test.txt", input); 
system.out.println(flag); 
} catch (unsupportedencodingexception e) { 
e.printstacktrace(); 
} 
}<pre></pre> 
@test
public void testuploadfromstring(){
try {
inputstream input = new bytearrayinputstream("test ftp".getbytes("utf-8"));
boolean flag = uploadfile("127.0.0.1", 21, "test", "test", "d:/ftp", "test.txt", input);
system.out.println(flag);
} catch (unsupportedencodingexception e) {
e.printstacktrace();
}
} 

二、下载文件

从ftp服务器下载文件的代码也很简单,参考如下:

/** 
* description: 从ftp服务器下载文件 
* @version. jul , :: pm by 崔红保(cuihongbao@d-heaven.com)创建 
* @param url ftp服务器hostname 
* @param port ftp服务器端口 
* @param username ftp登录账号 
* @param password ftp登录密码 
* @param remotepath ftp服务器上的相对路径 
* @param filename 要下载的文件名 
* @param localpath 下载后保存到本地的路径 
* @return 
*/ 
publicstaticboolean downfile(string url, int port,string username, string password, string remotepath,string filename,string localpath) { 
boolean success = false; 
ftpclient ftp = new ftpclient(); 
try { 
int reply; 
ftp.connect(url, port); 
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接ftp服务器 
ftp.login(username, password);//登录 
reply = ftp.getreplycode(); 
if (!ftpreply.ispositivecompletion(reply)) { 
ftp.disconnect(); 
return success; 
} 
ftp.changeworkingdirectory(remotepath);//转移到ftp服务器目录 
ftpfile[] fs = ftp.listfiles(); 
for(ftpfile ff:fs){ 
if(ff.getname().equals(filename)){ 
file localfile = new file(localpath+"/"+ff.getname()); 
outputstream is = new fileoutputstream(localfile); 
ftp.retrievefile(ff.getname(), is); 
is.close(); 
} 
} 
ftp.logout(); 
success = true; 
} catch (ioexception e) { 
e.printstacktrace(); 
} finally { 
if (ftp.isconnected()) { 
try { 
ftp.disconnect(); 
} catch (ioexception ioe) { 
} 
} 
} 
return success; 
}<pre></pre> 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网