javaftp下载文件判断重复|java如何测试连接ftp是否通

|

Ⅰ java如何测试连接ftp是否通

java测试连接ftp是否连通可以使用判断是否有异常来决定,实例如下:

/***connectServer*连接ftp服务器*@throwsjava.io.IOException*@parampath文件夹,空代表根目录*@parampassword密码*@paramuser登陆用户*@paramserver服务器地址*/publicvoidconnectServer(Stringserver,Stringuser,Stringpassword,Stringpath)throwsIOException{//server:FTP服务器的IP地址;user:登录FTP服务器的用户名//password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径ftpClient=newFtpClient();ftpClient.openServer(server);ftpClient.login(user,password);//path是ftp服务下主目录的子目录if(path.length()!=0)ftpClient.cd(path);//用2进制上传、下载ftpClient.binary();}/***upload*上传文件*@throwsjava.lang.Exception*@return-1文件不存在*-2文件内容为空*>0成功上传,返回文件的大小*@paramnewname上传后的新文件名*@paramfilename上传的文件*/publiclongupload(Stringfilename,Stringnewname)throwsException{longresult=0;TelnetOutputStreamos=null;FileInputStreamis=null;try{java.io.Filefile_in=newjava.io.File(filename);if(!file_in.exists())return-1;if(file_in.length()==0)return-2;os=ftpClient.put(newname);result=file_in.length();is=newFileInputStream(file_in);byte[]bytes=newbyte[1024];intc;while((c=is.read(bytes))!=-1){os.write(bytes,0,c);}}finally{if(is!=null){is.close();}if(os!=null){os.close();}}returnresult;}/***upload*@throwsjava.lang.Exception*@return*@paramfilename*/publiclongupload(Stringfilename)throwsException{Stringnewname="";if(filename.indexOf("/")>-1){newname=filename.substring(filename.lastIndexOf("/")+1);}else{newname=filename;}returnupload(filename,newname);}/***download*从ftp下载文件到本地*@throwsjava.lang.Exception*@return*@paramnewfilename本地生成的文件名*@paramfilename服务器上的文件名*/publiclongdownload(Stringfilename,Stringnewfilename)throwsException{longresult=0;TelnetInputStreamis=null;FileOutputStreamos=null;try{is=ftpClient.get(filename);java.io.Fileoutfile=newjava.io.File(newfilename);os=newFileOutputStream(outfile);byte[]bytes=newbyte[1024];intc;while((c=is.read(bytes))!=-1){os.write(bytes,0,c);result=result+c;}}catch(IOExceptione){e.printStackTrace();}finally{if(is!=null){is.close();}if(os!=null){os.close();}}returnresult;}/***取得某个目录下的所有文件列表**/publicListgetFileList(Stringpath){Listlist=newArrayList();try{DataInputStreamdis=newDataInputStream(ftpClient.nameList(path));Stringfilename="";while((filename=dis.readLine())!=null){list.add(filename);}}catch(Exceptione){e.printStackTrace();}returnlist;}/***closeServer*断开与ftp服务器的链接*@throwsjava.io.IOException*/publicvoidcloseServer()throwsIOException{try{if(ftpClient!=null){ftpClient.closeServer();}}catch(IOExceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args)throwsException{FtpUtilftp=newFtpUtil();try{//连接ftp服务器ftp.connectServer("10.163.7.15","cxl","1","info2");/**上传文件到info2文件夹下*/System.out.println("filesize:"+ftp.upload("f:/download/Install.exe")+"字节");/**取得info2文件夹下的所有文件列表,并下载到E盘下*/Listlist=ftp.getFileList(".");for(inti=0;i<list.size();i++){Stringfilename=(String)list.get(i);System.out.println(filename);ftp.download(filename,"E:/"+filename);}}catch(Exceptione){///}finally{ftp.closeServer();}}}

Ⅱ 如何判断FTP服务器上的文件,文件头都是一样的后缀不一样,怎么把所有文件头一样的文件都下载下来

用VB 跟API,现提示下思路 先连接 用OpenConnection然后 进入FTP 遍历文件夹 (FtpFindFirstFile,InternetFindNextFile)得到每项文件后,然后通过"." (instr(),mid()) 截取你想下载的文件头然后用下载 (FTPGetFile) 有些要引用API 函数。


赞 (0)