最新java做的比较完善的FTP上传下载文件服务器源码.docx
《最新java做的比较完善的FTP上传下载文件服务器源码.docx》由会员分享,可在线阅读,更多相关《最新java做的比较完善的FTP上传下载文件服务器源码.docx(9页珍藏版)》请在冰豆网上搜索。
最新java做的比较完善的FTP上传下载文件服务器源码
【最新】java做的比较完善的FTP上传下载文件服务器源码
Filename:
ftp.java
Author:
leetsing(elove)
Createdate:
2004-08-30
Use:
connecttoFTPserver,thenuploadanddownloadfile
Modifydate:
2004-09-05addtouploadfile
2004-09-13addtodownloadfile
Copyright:
MagiskyMediaTechnologyCo.,Ltd.
*******************************************************************************************************
字串7
*/
//importcz.dhl.io.*;
//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="lee";
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);
}
}
//上传文件
if(ft.aftp!
=null){
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\\");
}
//删除subunsubfromsp目录下已经上传的文件文件字串6
//ft.deleFile(RWFileDir);
//断开服务器连接
ft.stop(RWFileDir);
}
publicFtpClientconnect(StringRWFileDir,Stringhostname,intport,Stringuid,Stringpwd)
{
this.hostname=hostname;
System.out.println("正在连接"+hostname+",请等待.....");
try{
aftp=newFtpClient(hostname,port);
aftp.login(uid,pwd);
aftp.binary();
//aftp.openPortDataConnection();
a="连接主机:
"+hostname+"成功!
";
System.out.println(a);
}
catch(FtpLoginExceptione){
a="登陆主机:
"+hostname+"失败!
请检查用户名或密码是否正确:
"+e;
System.out.println(a);
//returnfalse;
}
catch(IOExceptione){
a="连接主机:
"+hostname+"失败!
请检查端口是否正确:
"+e;字串3
System.out.println(a);
//returnfalse;
}
catch(SecurityExceptione)
{
a="无权限与主机:
"+hostname+"连接!
请检查是否有访问权限:
"+e;
System.out.println(a);
//returnfalse;
}
log(RWFileDir,a);
returnaftp;
}
publicvoidstop(StringRWFileDir)
{
Stringmessage="";
try{
if(aftp!
=null){
aftp.closeServer();
message="与主机"+hostname+"连接已断开!
";
System.out.println(message);
log(RWFileDir,message);
}
}
catch(IOExceptione)
{
message="与主机"+hostname+"断开连接失败!
"+e;字串2
System.out.println(message);
log(RWFileDir,message);
}
}
publicbooleandownloadFile(StringRWFileDir,Stringfilepathname){
booleanresult=true;
Stringmessage="";
if(aftp!
=null)
{
System.out.println("正在下载文件"+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{
baddir="bad\\";
}字串9
Stringstrdir="subunsubtosp\\";
//System.out.println(RWFileDir+baddir+filepathname);
try{
//FtpClientfc=newFtpClient("192.168.0.56",2121);
//fc.login("lee","lee");
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();
message="下载"+filepathname+"文件到"+baddir+"目录成功!
";
System.out.println(message);
log(RWFileDir,message);
}
catch(IOExceptione){
message="下载"+filepathname+"文件到"+baddir+"目录失败!
"+e;
System.out.println(message);
log(RWFileDir,message);
result=false;
}
}
else{
result=false;
}
returnresult;
}
publicbooleanuploadFile(StringRWFileDir,Stringfilepathname){
booleanresult=true;字串8
Stringmessage="";
if(aftp!
=null)
{
System.out.println("正在上传文件"+filepathname+",请等待....");
try{
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);字串8
while(sendFile.getFilePo