您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 销售管理 > 【最新】java做的比较完善的FTP上传下载文件服务器源码
Filename:ftp.javaAuthor:leetsing(elove)Createdate:2004-08-30Use:connecttoFTPserver,thenuploadanddownloadfileModifydate:2004-09-05addtouploadfile2004-09-13addtodownloadfileCopyright:MagiskyMediaTechnologyCo.,Ltd.*******************************************************************************************************字串7*///importcz.dhl.io.*;//importcz.dhl.ftp.*;importsun.net.ftp.*;importsun.net.*;importjava.applet.*;importjava.io.*;importjava.io.IOException;importjava.util.StringTokenizer;importsun.net.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;字串3System.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;字串2System.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((badfile.compareTo(.bad)!=0)&&(badlog.compareTo(.badlog)!=0)){baddir=subunsubtosp\\;}else{baddir=bad\\;}字串9Stringstrdir=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();字串1fget.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;字串8Stringmessage=;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);字串8while(sendFile.getFilePointer()sendFile.length()){ch=sendFile.read();outputs.write(ch);}rename(filename.substring(15,filename.length()),filename.substring(20,filename.length()));outs.close();sendFile.close();message=上传+filepathname+文件成功!;System.out.println(message);log(RWFileDir,message);}catch(IOExceptione){message=上传+filepathname+文件失败!+e;System.out.println(message);log(RWFileDir,message);result=false;字串1}}else{result=false;}returnresult;}publicvoidrename(StringoldName,StringnewName){//aftp.renameTo(oldName,newName);FileOld=newFile(oldName);//oldNameFileNew=newFile(newName);//newName//aftp.renameTo(New);//booleanOld.renameTo(FilenewName);//System.out.println(Old);//System.out.println(New);}publicstaticvoiddeleFile(StringRWFileDir){//try{//取得ReadFile目录下的txt文件Stringsdir=RWFileDir+subunsubfromsp\\;Filefdir=newFile(sdir);StringFileName=;intj=fdir.list().length;System.out.println(sdir+目录下要删除的文件数:+fdir.list().length);字串6Filefile;for(inti=0;i{//删除subunsubfromsp中的txt文件FileName
本文标题:【最新】java做的比较完善的FTP上传下载文件服务器源码
链接地址:https://www.777doc.com/doc-1582019 .html