一个简单的FTP客户端 实现上传下载

package pub.ftp;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

/***/ /**
*FTPClient端调用模块
*作者:
*版本:1.0
*时间:2007.07.30
*/


public class FTPClient ... {
/***//**
*类的初始化,建立ftp的连接,用户登录,指定ftp的传输流
*
@paramhost
*
@paramport
*
@paramuser
*
@parampsw
*/

//建立一条与指定主机、指定端口上的FTP服务器的连接
privateFtpClientaftp=newFtpClient();;
privateDataOutputStreamoutputs;
privateTelnetOutputStreamouts;
privateTelnetInputStreaminps;


publicvoidFTPClass(Stringhost,intport,Stringuser,Stringpsw)...{

try...{


//注册到FTP服务器
aftp.openServer(host);
System.out.println(
"登陆....");
aftp.login(user,psw);

System.out.println(
"登陆FTP服务器成功!");
aftp.binary();

}
catch(IOExceptione)...{

System.out.println(
"连接FTP服务器失败!");
e.printStackTrace();
}



}

/***//**
*通过ftp上传文件到服务器上
*
@paramlocalFile本地所要上传的文件
*
@paramremoteFile传到服务器上的文件名称
*/

publicbooleanupFile(StringlocalFile,StringremoteFile)...{
booleanresult=true;
if(aftp!=null)...{
System.out.println(
"正在上传文件"+localFile+",请等待....");
try...{
Filefile
=newFile(localFile);
outs
=aftp.put(remoteFile);
FileInputStreamin
=newFileInputStream(file);
byte[]bytes=newbyte[1024];
intc;
while((c=in.read(bytes))!=-1)...{
outs.write(bytes,
0,c);
}

outs.close();
in.close();
System.out.println(
"上传文件"+localFile+"成功!");
System.out.println(
"上传文件所在目录:"+remoteFile+"");
}
catch(Exceptione)...{
e.printStackTrace();
System.out.println(
"上传文件"+localFile+"失败!");
result
=false;
}


}
else...{
result
=false;
}

returnresult;
}

/***//**
*下载FTP服务器上的文件
*
@paramlocalFile本地文件名
*
@paramremoteFile远程服务器文件名
*/

publicbooleandownFile(StringremoteFile,StringlocalFile)...{
booleanresult=true;
if(aftp!=null)...{
System.out.println(
"正在下载文件"+remoteFile+",请等待....");
try...{
Filefile
=newFile(remoteFile);
inps
=aftp.get(remoteFile);
aftp.cd(
"D;/jiadong");
FileInputStreamin
=newFileInputStream(file);
//RandomAccessFilegetFile=newRandomAccessFile(file,"rw");
//getFile.seek(0);
FileOutputStreamos=newFileOutputStream(file);

byte[]bytes=newbyte[1024];
intc;
while((c=in.read(bytes))!=-1)...{
//.write(bytes,0,c);
os.write(bytes,0,c);

}

inps.close();
in.close();
os.close();
System.out.println(
"下载文件"+remoteFile+"成功!");
System.out.println(
"上传文件所在目录:"+localFile+"");
}
catch(Exceptione)...{
e.printStackTrace();
System.out.println(
"下载文件"+remoteFile+"失败!");
result
=false;
}

}

returnfalse;
}

/***//**
*断开ftp连接
*
@throwsIOException
*
*/

publicvoiddisconnect()throwsIOException...{
aftp.closeServer();
System.out.println(
"FTP服务器连接断开!");
}

//返回当前目录的所有文件及文件夹

publicArrayListgetFileList()throwsIOException...{
BufferedReaderdr
=newBufferedReader(newInputStreamReader(aftp.list()));
ArrayListal
=newArrayList();
Strings
="";
while((s=dr.readLine())!=null)...{
al.add(s);
}

returnal;
}


publicstaticvoidmain(String[]args)throwsIOException...{
FTPClientftpClient
=newFTPClient();
ftpClient.FTPClass(
"192.168.8.154",21,"jiadong","jiadong");
//ftpClient.upFile("F:/music/想念.mp3","/jia/hehe.mp3");

//ftpClient.upFile("d:/jiadong","D:/workspace/msgfield.xml");
ftpClient.downFile("hehe.xml","/jia/ruru.xml");
ftpClient.disconnect();


}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值