TAG:
Java写的支持断点续传的FTP 很不错的值得研究一下,结构比较清楚。 基类一: package ming.ftpsearch; public class FileInfo { public String name; public String description; public FileInfo(String nm,String dscrp) { name=nm; description=dscrp; } } 基类二: package ming.ftpsearch; import java.io.*; import java.net.*; import java.util.*; public class FTPSearch extends Thread { String _host; static final int PORT=21; ServerSocket ss; Socket _ds; Socket _sc; PrintStream _out ; String _hostAddress; BufferedReader _in ; BufferedReader _dataIn; String _reply; protected StringBuffer _log=new StringBuffer(10000); Vector _result; String _user; String _pass; public FTPSearch (String ip,String user,String pass) { _host=ip; _user=user; _pass=pass; _result=new Vector(); } public FTPSearch (String ip) { _host=ip; _user="anonymous"; _pass="name_zm@sohu.com"; _result=new Vector(); } protected boolean login()throws UnknownHostException,IOException { boolean successful=false; StringBuffer reply=new StringBuffer(); _sc=new Socket(_host,PORT); _hostAddress=_sc.getLocalAddress().getHostAddress().replace(''.'','',''); _sc.setSoTimeout(15000); _out = new PrintStream(_sc.getOutputStream(),true); _in = new BufferedReader(new InputStreamReader(_sc.getInputStream())); reply.append(readReply().trim()); if(reply.toString().startsWith("220")) if(comm("USER "+_user)) if(comm("PASS "+_pass)) successful=true; comm("TYPE A"); return successful; } protected String getCurrentDir()throws IOException { String currentDir=null; if(comm("PWD")) { StringTokenizer st=new StringTokenizer(_reply); st.nextToken(); StringBuffer rtDir=new StringBuffer(st.nextToken()); currentDir=rtDir.substring(1,rtDir.length()-1); } return currentDir; } public void search()throws IOException,InterruptedException { try { for(int i=0;i<5;i++) { if(login())break; System.out.println("Wait 10 seconds to try again..."); sleep(10000); } scan(); logout(); } catch(IOException ex) { } finally { PrintStream out=new PrintStream(new FileOutputStream("Log.txt")); out.println(_log); out.close(); printResult("result.txt"); } } (iwgh) |