哪儿有java写的ftp搜索引擎的例子? www.java-cn.netwww.chinajavaworld.com 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 自动力从网上搜索,建立数据库,再从数据库检索。就是一般的ftp搜索引擎的功能就行。 到www.google.com 搜索吧很多的。 效率不高吧?我感觉用java来做这样的应用,资源和效率是大问题... 看看这个吧:版权申明:非本人创作,乃网友所创package mshtang.ftp;import java.io.*;import java.net.*;import java.util.*;/** * <p>Title: FTPSearch</p> * <p>Description: 本类可以搜索某个FTP站点的内容,要求给出站点IP地址。</p> * <p>Copyright: 厦门一方软件公司版权所有Copyright (c) 2002</p> * <p>Company: 厦门一方软件公司</p> * @author 小唐蔡 * @version 1.0 */public class FTPSearch extends Thread{ /**主机ip地址*/ 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; /**构造函数。<br>参数:ip为远端主机ip地址,user为用户名,pass为口令。*/ public FTPSearch(String ip,String user,String pass) { _host = ip; _user = user; _pass = pass; _result = new Vector(); } /**构造函数。<br>参数:ip为远端主机ip地址,匿名登录。*/ public FTPSearch(String ip) { _host = ip; _user = "lq"; _pass = "linquan"; _result = new Vector(); } /**连接登录<br>Return:<br>true:登录成功;false:登录失败.*/ 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; } /**列出站点中所有的目录、子目录和文件,存入_result属性变量,和一个名为“result.txt”的文件。 *<br>并产生一个名为”Log.txt“日志文件。*/ 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"); } } protected void scan()throws IOException { Vector fileNames = new Vector(); tellPort(); comm("LIST -R"); //Get data_connection's InputStream. try { _ds = ss.accept(); System.out.println(ss.toString()); } catch(IOException ex) { ss.close(); } _dataIn = new BufferedReader(new InputStreamReader(_ds.getInputStream())); System.out.println("Scanning now.Please waiting......"); //Read directory content try { String temp=""; do { temp = _dataIn.readLine(); if(temp == null) { break; } fileNames.add(temp); //System.out.println(temp); } while(temp != null); } catch(InterruptedIOException ex) { System.out.println("Caught InterruptedIOException1:"+ex);//如果超时则继续 } //Read end infomation try { do { String temp = _in.readLine(); _log.append(temp + "\r\n"); System.out.println(temp); } while(_in.ready()); } catch(InterruptedIOException ex) { System.out.println("Caught InterruptedIOException2:"+ex);//如果超时则继续 } _result = fileNames; } /**得到搜索结果向量。<br>记住要在执行search()方法后执行。*/ public Vector getResult() { return _result; } /**得到搜索日志。<br>记住要在执行search()方法后执行。*/ public StringBuffer getLog() { return _log; } 续:/**分析得到的_result各成员,分辨目录、文件、及其他*/ public static Vector parse()throws FileNotFoundException,IOException { Vector result = new Vector(); FileInputStream fileIn = new FileInputStream("result.txt"); BufferedReader fReader = new BufferedReader(new InputStreamReader(fileIn)); //int size=fileIn.available(); //System.out.println(String.valueOf(size)); String dir = "/"; System.out.println("Parsing now.Please wait......"); while(fReader.ready()) { String temp = fReader.readLine(); StringTokenizer tt = new StringTokenizer(temp); if(tt.countTokens() == 1) { if(temp.startsWith("/")) { dir = temp.replace(':',' ').trim() + "/"; } else { dir = "/"+temp.replace(':',' ').trim() + "/"; } //parseResult.add(dir); continue; } if((!temp.equals("")) && (temp.startsWith("-") || temp.startsWith("b") || temp.startsWith("c") || temp.startsWith("l"))) //是文件就加上dir加入parseResult. { for(int i = 0; i < 4; i++) { tt.nextToken(); } String description=""; for(int i = 0; i < 4; i++) { description = description+tt.nextToken()+" "; } String cont=""; do { cont=cont+tt.nextToken()+" "; } while(tt.hasMoreTokens()); result.add(new FileInfo(dir+cont.trim(),description.trim())); continue; } else if(temp.startsWith("d"))//是目录 { for(int i=0;i<4;i++) { tt.nextToken(); } String description=""; for(int i=0;i<4;i++) { description=description+tt.nextToken()+" "; } String cont=""; do { cont=cont+tt.nextToken()+" "; } while(tt.hasMoreTokens()); result.add(new FileInfo(dir+cont.trim()+"/",description.trim())); continue; } else if(temp.equals("")) { continue; } } return result; } protected void logout()throws IOException { comm("QUIT"); } protected void printResult(String resultFile)throws IOException { PrintStream out=new PrintStream(new FileOutputStream(resultFile));//会自动创建一个文件 int len=_result.size(); for (int i=0;i<len;i++) { out.println(_result.elementAt(i)); } out.close(); } protected boolean comm(String command)throws IOException { boolean success = false; _out.println(command); System.out.println("<COMMAND>"+command); _log.append("<COMMAND>"+command+"\r\n"); _reply = readReply(); if(command.startsWith("USER")) { success=_reply.startsWith("331")?true:false; } else if(command.startsWith("PASS")) { success=_reply.startsWith("230")?true:false; try { readReply(); } catch(InterruptedIOException ex){} } else if(command.equals("TYPE A")) { success=_reply.startsWith("200")?true:false; } else if(command.startsWith("PORT")) { success=_reply.startsWith("200")?true:false; } else if(command.startsWith("LIST")) { success=_reply.startsWith("150")?true:false; } else if(command.startsWith("NLST")) { success=_reply.startsWith("150")?true:false; } else if(command.equals("PWD")) { success=_reply.startsWith("257")?true:false; } else if(command.startsWith("STAT")) { success=_reply.startsWith("211")?true:false; } else if(command.equals("SYST")) { success=_reply.startsWith("215")?true:false; } else if(command.startsWith("SITE")) { success=_reply.startsWith("214")?true:false; } else if(command.equals("QUIT")) { success=_reply.startsWith("221")?true:false; } return success; } /**把指定向量中的内容在标准输出打印出来。*/ public static void printVector(Vector vct) { int size=vct.size(); for(int i=0;i<size;i++) { System.out.println(vct.elementAt(i)); } } protected String readReply()throws IOException, InterruptedIOException { StringBuffer reply=new StringBuffer(); do { reply.append(_in.readLine()+"\r\n"); } while(_in.ready()); System.out.println(reply); _log.append(reply); _reply=reply.toString(); return _reply; } private int getDataPort() { int port=_sc.getLocalPort()+1; return port; } private void tellPort()throws IOException { try { int port=getDataPort(); System.out.println("Get data port:"+port); int x=(int) Math.round((port/256)-0.5); int y=port-x*256; try { ss=new ServerSocket(port); } catch(IOException ex) { System.out.println("IOException in Method tellPort() in Class ftpc--"+ex); } comm("PORT "+_hostAddress+","+x+","+y); } finally { } } public static void main(String arg[])//throws IOException,UnknownHostException,InterruptedException { try { FTPSearch client=new FTPSearch("192.168.0.33", "lq", "linquan"); client.search(); client.parse(); } catch(Exception ex)//probably catch IOException,UnknownHostException,InterruptedException { System.out.println(ex); } }}结束 to Tmsheng(小唐蔡)非常感谢!分是你的了! 静态方法/变量 String s = "abc"; 栈中的"abc"是String类的对象吗? java...新手接口問題... 利用swing+管道实现与cmd通信 在java中怎么通过按钮实现一个计时器功能! 急:怎么让出售的CLASS只能运行在一台电脑上?防止COPY 为什么Boolean.getBoolean("true")的结果是false????? java 怎么分析GBK的xml 内部类问题?????? Java编程思想第八章的一个示例,为什么我运行无结果? 急急,各位高手救救我!!! 最简单的JNI问题请教
就是一般的ftp搜索引擎的功能就行。
我感觉用java来做这样的应用,资源和效率是大问题...
package mshtang.ftp;
import java.io.*;
import java.net.*;
import java.util.*;
/**
* <p>Title: FTPSearch</p>
* <p>Description: 本类可以搜索某个FTP站点的内容,要求给出站点IP地址。</p>
* <p>Copyright: 厦门一方软件公司版权所有Copyright (c) 2002</p>
* <p>Company: 厦门一方软件公司</p>
* @author 小唐蔡
* @version 1.0
*/
public class FTPSearch extends Thread
{
/**主机ip地址*/
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;
/**构造函数。<br>参数:ip为远端主机ip地址,user为用户名,pass为口令。*/
public FTPSearch(String ip,String user,String pass)
{
_host = ip;
_user = user;
_pass = pass;
_result = new Vector();
} /**构造函数。<br>参数:ip为远端主机ip地址,匿名登录。*/
public FTPSearch(String ip)
{
_host = ip;
_user = "lq";
_pass = "linquan";
_result = new Vector();
} /**连接登录<br>Return:<br>true:登录成功;false:登录失败.*/
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;
} /**列出站点中所有的目录、子目录和文件,存入_result属性变量,和一个名为“result.txt”的文件。
*<br>并产生一个名为”Log.txt“日志文件。*/
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");
} }
protected void scan()throws IOException
{
Vector fileNames = new Vector();
tellPort();
comm("LIST -R"); //Get data_connection's InputStream.
try
{
_ds = ss.accept();
System.out.println(ss.toString());
}
catch(IOException ex)
{
ss.close();
}
_dataIn = new BufferedReader(new InputStreamReader(_ds.getInputStream())); System.out.println("Scanning now.Please waiting......");
//Read directory content
try
{
String temp="";
do
{
temp = _dataIn.readLine();
if(temp == null)
{
break;
}
fileNames.add(temp);
//System.out.println(temp);
}
while(temp != null);
}
catch(InterruptedIOException ex)
{
System.out.println("Caught InterruptedIOException1:"+ex);//如果超时则继续
} //Read end infomation
try
{
do
{
String temp = _in.readLine();
_log.append(temp + "\r\n");
System.out.println(temp);
}
while(_in.ready());
}
catch(InterruptedIOException ex)
{
System.out.println("Caught InterruptedIOException2:"+ex);//如果超时则继续
}
_result = fileNames;
} /**得到搜索结果向量。<br>记住要在执行search()方法后执行。*/
public Vector getResult()
{
return _result;
} /**得到搜索日志。<br>记住要在执行search()方法后执行。*/
public StringBuffer getLog()
{
return _log;
}
/**分析得到的_result各成员,分辨目录、文件、及其他*/
public static Vector parse()throws FileNotFoundException,IOException
{
Vector result = new Vector();
FileInputStream fileIn = new FileInputStream("result.txt");
BufferedReader fReader = new BufferedReader(new InputStreamReader(fileIn));
//int size=fileIn.available();
//System.out.println(String.valueOf(size));
String dir = "/";
System.out.println("Parsing now.Please wait......");
while(fReader.ready())
{
String temp = fReader.readLine();
StringTokenizer tt = new StringTokenizer(temp); if(tt.countTokens() == 1)
{
if(temp.startsWith("/"))
{
dir = temp.replace(':',' ').trim() + "/";
}
else
{
dir = "/"+temp.replace(':',' ').trim() + "/";
}
//parseResult.add(dir);
continue;
}
if((!temp.equals("")) && (temp.startsWith("-") || temp.startsWith("b") || temp.startsWith("c") || temp.startsWith("l")))
//是文件就加上dir加入parseResult.
{
for(int i = 0; i < 4; i++)
{
tt.nextToken();
}
String description="";
for(int i = 0; i < 4; i++)
{
description = description+tt.nextToken()+" ";
}
String cont="";
do
{
cont=cont+tt.nextToken()+" ";
}
while(tt.hasMoreTokens());
result.add(new FileInfo(dir+cont.trim(),description.trim()));
continue;
}
else if(temp.startsWith("d"))//是目录
{
for(int i=0;i<4;i++)
{
tt.nextToken();
}
String description="";
for(int i=0;i<4;i++)
{
description=description+tt.nextToken()+" ";
}
String cont="";
do
{
cont=cont+tt.nextToken()+" ";
}
while(tt.hasMoreTokens());
result.add(new FileInfo(dir+cont.trim()+"/",description.trim()));
continue;
}
else if(temp.equals(""))
{
continue;
}
}
return result;
} protected void logout()throws IOException
{
comm("QUIT");
} protected void printResult(String resultFile)throws IOException
{
PrintStream out=new PrintStream(new FileOutputStream(resultFile));//会自动创建一个文件
int len=_result.size();
for (int i=0;i<len;i++)
{
out.println(_result.elementAt(i));
}
out.close();
}
protected boolean comm(String command)throws IOException
{
boolean success = false;
_out.println(command);
System.out.println("<COMMAND>"+command);
_log.append("<COMMAND>"+command+"\r\n");
_reply = readReply();
if(command.startsWith("USER"))
{
success=_reply.startsWith("331")?true:false;
}
else if(command.startsWith("PASS"))
{
success=_reply.startsWith("230")?true:false;
try
{
readReply();
}
catch(InterruptedIOException ex){}
}
else if(command.equals("TYPE A"))
{
success=_reply.startsWith("200")?true:false;
}
else if(command.startsWith("PORT"))
{
success=_reply.startsWith("200")?true:false;
}
else if(command.startsWith("LIST"))
{
success=_reply.startsWith("150")?true:false;
}
else if(command.startsWith("NLST"))
{
success=_reply.startsWith("150")?true:false;
}
else if(command.equals("PWD"))
{
success=_reply.startsWith("257")?true:false;
}
else if(command.startsWith("STAT"))
{
success=_reply.startsWith("211")?true:false;
}
else if(command.equals("SYST"))
{
success=_reply.startsWith("215")?true:false;
}
else if(command.startsWith("SITE"))
{
success=_reply.startsWith("214")?true:false;
}
else if(command.equals("QUIT"))
{
success=_reply.startsWith("221")?true:false;
}
return success;
}
/**把指定向量中的内容在标准输出打印出来。*/
public static void printVector(Vector vct)
{
int size=vct.size();
for(int i=0;i<size;i++)
{
System.out.println(vct.elementAt(i));
}
}
protected String readReply()throws IOException, InterruptedIOException
{
StringBuffer reply=new StringBuffer();
do
{
reply.append(_in.readLine()+"\r\n");
}
while(_in.ready());
System.out.println(reply);
_log.append(reply);
_reply=reply.toString();
return _reply;
} private int getDataPort()
{
int port=_sc.getLocalPort()+1;
return port;
} private void tellPort()throws IOException
{
try
{
int port=getDataPort();
System.out.println("Get data port:"+port);
int x=(int) Math.round((port/256)-0.5);
int y=port-x*256;
try
{
ss=new ServerSocket(port);
}
catch(IOException ex)
{
System.out.println("IOException in Method tellPort() in Class ftpc--"+ex);
}
comm("PORT "+_hostAddress+","+x+","+y);
}
finally
{
}
}
public static void main(String arg[])//throws IOException,UnknownHostException,InterruptedException
{
try
{
FTPSearch client=new FTPSearch("192.168.0.33", "lq", "linquan");
client.search();
client.parse();
}
catch(Exception ex)//probably catch IOException,UnknownHostException,InterruptedException
{
System.out.println(ex);
}
}
}
结束
非常感谢!分是你的了!