先我们来看两段简单的代码
将本地文件上传到ftp服务器上,代码如下:
@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();
}
}
在ftp服务器上生成一个文件,并将一个字符串写入到该文件中
@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();
}
}
完整实例
import java.io.file;
import it.sauronsoftware.ftp4j.ftpclient;
import it.sauronsoftware.ftp4j.ftpfile;public class javaftp {
private ftpclient client =null;
public void init(){
try {
client = new ftpclient();
client.connect("192.168.1.248", 21);
client.login("db2inst1", "db2inst1");
} catch (exception e) {
e.printstacktrace();
}
}
public static void main(string[] args) {
try {
javaftp javaftp=new javaftp();
javaftp.init();
javaftp.download("e://test", "/test/");
javaftp.close();
} catch (exception e) {
e.printstacktrace();
}
}
public void download(string localpath,string clientpath){
try {
if(clientpath!=null&&clientpath.length()>=0){
client.changedirectory(clientpath);
}
ftpfile[] list = client.list();for(int i=0;i<list.length;i++){
ftpfile file = list[i];
system.out.println(file.gettype());
if(file.gettype()==ftpfile.type_directory){
file temp=new file(localpath+file.separator+file.getname());
if(!temp.exists()){
temp.mkdir();
}
download(localpath+file.separator+file.getname(),file.getname());
}
else if(file.gettype()==ftpfile.type_file){
file localfile=new file(localpath+file.separator+file.getname());
client.download(file.getname(), localfile);
}
}
} catch (exception e) {
e.printstacktrace();
}
}
public void close(){
try {
client.disconnect(true);
} catch (exception e) {
e.printstacktrace();
}
}
}
从ftp服务器下载文件
* @version1.0 jul 27, 2008 5:32:36 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
*/
public static boolean 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;
}