最新java做的比较完善的FTP上传下载文件服务器源码Word文档下载推荐.docx
《最新java做的比较完善的FTP上传下载文件服务器源码Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《最新java做的比较完善的FTP上传下载文件服务器源码Word文档下载推荐.docx(9页珍藏版)》请在冰豆网上搜索。
//importcz.dhl.ftp.*;
import.ftp.*;
import.*;
importjava.applet.*;
importjava.io.*;
importjava.io.IOException;
importjava.util.StringTokenizer;
import.ftp.FtpClient;
importjava.util.ArrayList;
publicclassftpextendsApplet
{
FtpClientaftp;
DataOutputStreamoutputs;
TelnetInputStreamins;
TelnetOutputStreamouts;
intch;
publicStringa;
Stringhostname="
"
;
privateStringpath="
/"
publicstaticvoidmain(String[]args)
Stringhostname="
192.168.0.56"
intport=2121;
Stringuid="
lee"
Stringpwd="
StringRWFileDir="
D:
\\smsftp\\"
//文件目录
//连接ftp服务器
ftpft=newftp();
ft.connect(RWFileDir,hostname,port,uid,pwd);
字串3
//下载文件
if(ft.aftp!
=null){
try{
ft.getNameList(RWFileDir);
}catch(IOExceptione)
{
System.out.println("
下载文件出错:
+e);
}
//上传文件
Stringsdir=RWFileDir+"
subunsubfromsp\\"
Filefdir=newFile(sdir);
StringFileName="
for(inti=0;
iFileName=sdir+(fdir.list())[i];
ft.uploadFile(RWFileDir,FileName);
//System.out.println("
成功上传的文件:
);
//ft.showFileContents("
//删除subunsubfromsp目录下已经上传的文件文件字串6
//ft.deleFile(RWFileDir);
//断开服务器连接
ft.stop(RWFileDir);
}
publicFtpClientconnect(StringRWFileDir,Stringhostname,intport,Stringuid,Stringpwd)
this.hostname=hostname;
正在连接"
+hostname+"
,请等待....."
try{
aftp=newFtpClient(hostname,port);
aftp.login(uid,pwd);
aftp.binary();
//aftp.openPortDataConnection();
a="
连接主机:
成功!
System.out.println(a);
catch(FtpLoginExceptione){
a="
登陆主机:
失败!
请检查用户名或密码是否正确:
+e;
//returnfalse;
catch(IOExceptione){
请检查端口是否正确:
字串3
catch(SecurityExceptione)
无权限与主机:
连接!
请检查是否有访问权限:
log(RWFileDir,a);
returnaftp;
publicvoidstop(StringRWFileDir)
Stringmessage="
if(aftp!
=null){
aftp.closeServer();
message="
与主机"
连接已断开!
System.out.println(message);
log(RWFileDir,message);
catch(IOExceptione)
断开连接失败!
字串2
publicbooleandownloadFile(StringRWFileDir,Stringfilepathname){
booleanresult=true;
if(aftp!
=null)
正在下载文件"
+filepathname+"
请等待...."
Stringbadfile=filepathname.substring(filepathname.length()-4,filepathname.length());
Stringbadlog=filepathname.substring(filepathname.length()-7,filepathname.length());
Stringbaddir="
if((pareTo("
.bad"
)!
=0)&
&
(pareTo("
.badlog"
=0)){
baddir="
subunsubtosp\\"
else{
bad\\"
}字串9
Stringstrdir="
//System.out.println(RWFileDir+baddir+filepathname);
//FtpClientfc=newFtpClient("
2121);
//fc.login("
"
intch;
Filefi=newFile(RWFileDir+baddir+filepathname);
//aftp.cd(strdir);
RandomAccessFilegetFile=newRandomAccessFile(fi,"
rw"
getFile.seek(0);
TelnetInputStreamfget=aftp.get(strdir+filepathname);
DataInputStreamputs=newDataInputStream(fget);
while((ch=puts.read())>
=0){
getFile.write(ch);
//s.delete();
字串1
fget.close();
getFile.close();
//fc.closeServer();
下载"
文件到"
+baddir+"
目录成功!
catch(IOExceptione){
目录失败!
result=false;
result=false;
returnresult;
publicbooleanuploadFile(StringRWFileDir,Stringfilepathname){
字串8
正在上传文件"
Stringfg=newString("
\\subunsubfromsp\\"
intindex=filepathname.lastIndexOf(fg);
Stringfilename=filepathname.substring(index+1);
FilelocalFile=newFile(filepathname);
RandomAccessFilesendFile=newRandomAccessFile(filepathname,"
r"
//
sendFile.seek(0);
//改名上传temp_
filename=filename.substring(0,15)+"
temp_"
+filename.substring(15,filename.length());
outs=aftp.put(filename);
outputs=newDataOutputStream(outs);
while(sendFile.getFilePo