java做的比较完善的FTP连接上传下载文件.docx
- 文档编号:24470228
- 上传时间:2023-05-27
- 格式:DOCX
- 页数:14
- 大小:17.46KB
java做的比较完善的FTP连接上传下载文件.docx
《java做的比较完善的FTP连接上传下载文件.docx》由会员分享,可在线阅读,更多相关《java做的比较完善的FTP连接上传下载文件.docx(14页珍藏版)》请在冰豆网上搜索。
java做的比较完善的FTP连接上传下载文件
ÕâÊÇżµÚÒ»´ÎдJava³ÌÐòʵÏÖ¹«Ë¾ÒªÇóµÄFTPÊý¾Ýͬ²½£¬ËùÓÐÔ´úÂëÈçÏ£¬ÒÔ¹©¸÷λ²Î¿¼²¢Ìá³ö±¦¹óÒâ¼û£¡
ÁíÍâ°³»¹ÓÐÁ½¸ö¹¦ÄÜûÓÐʵÏÖ£¬ÓÐÇë¸ßÊÖÖ¸µãÒ»¶þ£º
1¡¢ÈçºÎɾ³ýFTP·þÎñÆ÷ÉÏÒѾÏÂÔعýµÄÎļþ£¿
2¡¢ÈçºÎ½«ÉÏ´«µÄÎļþ¸ÄÃû£¿ÉÏ´«Ê±ÔÚÎļþÃûÇ°¼Ó¡°temp_¡±£¬ÉÏ´«Íê±ÏºóÐèÒª°ÑÎļþÃû¸Ä»ØÀ´¡£
3¡¢ÈçºÎʹÓÃopenPassiveDataConnection()·½·¨½«ftpÁ¬½Ó¸ü¸ÄΪÖ÷¶¯Á¬½Ó£¬ÒòΪҪ´ÓunixϵͳÅäÖõÄftp·þÎñÆ÷ÏÂÔØÎļþµÄ»°£¬¾ÍÐèÒªÖ÷¶¯Á¬½Ó¡£
/*
*******************************************************************************************************
Filename:
ftp.java
Author:
leetsing(elove)
Createdate:
2004-08-30
Use:
connecttoFTPserver,thenuploadanddownloadfile
Modifydate:
2004-09-05addtouploadfile
2004-09-13addtodownloadfile
Copyright:
MagiskyMediaTechnologyCo.,Ltd.
*******************************************************************************************************
*/
//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);
//ÏÂÔØÎļþ
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Ŀ¼ÏÂÒѾÉÏ´«µÄÎļþÎļþ
//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;
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;
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\\";
}
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();
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;
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);
while(sendFile.getFilePointer() { 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; } } else{ result=false; } returnresult; } publicvoidrename(StringoldName,StringnewName){ //aftp.renameTo(oldName,newName); FileOld=newFile(oldName);//oldName FileNew=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); Filefile; for(inti=0;i{ //ɾ³ýsubunsubfromspÖеÄtxtÎļþ FileName=RWFileDir+"subunsubfromsp\\"+(fdir.list())[0]; file=newFile(FileName); file.delete(); System.out.println("ÒѾ³É¹¦É¾³ý"+FileName+"Îļþ£¡"); } //} //catch(IOExceptione){ //System.out.println("ɾ³ýtxtÎļþ´íÎó! "); //e.printStackTrace(); //} } publicvoidshowFileContents(Stringstrdir) { StringBufferbuf=newStringBuffer(); try{ aftp.cd(strdir); ins=aftp.list(); while((ch=ins.read())>=0){ buf.append((char)ch); } System.out.println(buf.toString()); ins.close(); } catch(IOExceptione) { } } //·µ»Øµ±Ç°Ä¿Â¼µÄËùÓÐÎļþ¼°Îļþ¼Ð publicArrayListgetFileList()throwsIOException{ BufferedReaderdr=newBufferedReader(newInputStreamReader(aftp.list())); ArrayListal=newArrayList(); Strings=""; while((s=dr.readLine())! =null){ al.add(s); } returnal; } publicvoidsetPath(Stringpath)throwsIOException{ if(aftp==null) this.path=path; else{ aftp.cd(path); } } //·µ»Øµ±Ç°Ä¿Â¼µÄÎļþÃû³Æ publicArrayListgetNameList(StringRWFileDir)throwsIOException{ BufferedReaderdr=newBufferedReader(newInputStreamReader(aftp.nameList("subunsubtosp\\"))); ArrayListal=newArrayList(); Strings=""; while((s=dr.readLine())! =null){ al.add(s); s=s.substring(13,s.length()); isFile(s); downloadFile(RWFileDir,s); //StringstrFileDelF=aftp.nameList("subunsubtosp\\"); FilefileDelF=newFile(s); fileDelF.delete(); } returnal; //System.out.println(al.add(s)); } //ÅжÏÒ»ÐÐÎļþÐÅÏ¢ÊÇ·ñΪĿ¼ publicbooleanisDir(Stringline){ return((String)parseLine(line).get(0)).indexOf("d")! =-1; } publicbooleanisFile(Stringline){ return! isDir(line); } //´¦ÀígetFileListÈ¡µÃµÄÐÐÐÅÏ¢ privateArrayListparseLine(Stringline){ ArrayLists1=newArrayList(); StringTokenizerst=newStringTokenizer(line,""); while(st.hasMoreTokens()){ s1.add(st.nextToken()); } returns1; } //дÏûÏ¢ÈÕÖ¾ publicstaticvoidlog(StringRWFileDir,Stringmsg) { Stringmessage=""; try{ java.text.DateFormatdf=newjava.text.SimpleDateFormat("yyyy-MM-ddHH: mm: ss"); java.text.DateFormatdflog=newjava.text.SimpleDateFormat("yyyyMMdd"); java.util.Datedate=newjava.util.Date(); Stringdatestr=df.format(newjava.util.Date()); Stringdatelog=dflog.format(newjava.util.Date()); //Stringdatelog=datestr.substring(0,10); //datelog=datelog.replace('-',''); //°´ÈÕÆÚÿÌìÉú³ÉÒ»¸öÈÕÖ¾Îļþ FileWriterfwl=newFileWriter(RWFileDir+"CMSSftp"+datelog+".log",true); PrintWriteroutl=newPrintWriter(fwl); outl.println(datestr+""+msg); outl.close(); fwl.close(); }catch(IOExceptione){ message="дlogÎļþ´íÎó! "+e; e.printStackTrace(); log(RWFileDir,message); System.out.println(message); } } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 比较 完善 FTP 连接 上传下载 文件
![提示](https://static.bdocx.com/images/bang_tan.gif)