DownloadTool.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:1k
源码类别:

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.tool.impl;
  2. import net.javacoding.jspider.api.event.JSpiderEvent;
  3. import net.javacoding.jspider.api.event.resource.ResourceFetchedEvent;
  4. import java.io.*;
  5. /**
  6.  * $Id: DownloadTool.java,v 1.3 2003/04/01 19:44:42 vanrogu Exp $
  7.  */
  8. public class DownloadTool extends BaseToolImpl {
  9.     public String getName() {
  10.         return "download";
  11.     }
  12.     public String getUsage() {
  13.         return "[filename]";
  14.     }
  15.     public int getParameterCount() {
  16.         return 1;
  17.     }
  18.     public void notify(JSpiderEvent event) {
  19.         String fileName = parameters.getValues()[0];
  20.         if ( event instanceof ResourceFetchedEvent ) {
  21.             ResourceFetchedEvent rfe = (ResourceFetchedEvent) event;
  22.             int counter = 0;
  23.             try {
  24.                 InputStream is = rfe.getResource().getInputStream();
  25.                 OutputStream os = new FileOutputStream(fileName);
  26.                 int read = is.read();
  27.                 while ( read != -1 ) {
  28.                     counter++;
  29.                     os.write(read);
  30.                     read = is.read ( );
  31.                 }
  32.                 os.flush();
  33.                 os.close();
  34.                 is.close();
  35.                 System.out.println("Downloaded resource to '" + fileName + "'  (" + counter + " bytes)" );
  36.             } catch (IOException e) {
  37.                 e.printStackTrace();
  38.             }
  39.         }
  40.     }
  41. }