java可不可以实现访问一个页面,然后把这个页面的内容save到本地
然后要访问10000次,save10000次,每次访问的页面格式相同,但是内容不同
就好像select语句的where条件不同
比如
where id = 0
where id = 1

解决方案 »

  1.   

    应该能做到,以前看到过一个抓取网页页面内容的例子,你去IBM的网站找找看
      

  2.   

    利用URLConnection或者是apache的HttpClient,都可以,后者功能更强大,前者使用比较简单。注意抓取的时候要分析页面的编码格式,否则抓出来的容易是乱码。
      

  3.   

    /**
     * @(#) copyURL.java
     *
     * Copyright 2004 Opensource Develop Team. All rights reserved.
     */// package
    package com.opensource.url;// import classes
    import java.net.*;
    import java.io.*;
    import java.util.Date;
    import java.util.StringTokenizer;/**
     * Utility for copying files from the Internet to local disk
     * Example: 1. java copyURL http://www.patriot.net/users/anil/resume/resume.gif
     *          
     *          2. java copyURL http://www.ibm.com/index.html abcd.html
     */
    public class copyURL
    {
         public static void main(String args[])
         {
               if (args.length < 1)
               {
                     System.err.println("usage: java copyURL URL [LocalFile]");
                     System.exit(1);
               }
               try
               {
                     URL url  = new URL(args[0]);
                     System.out.println("Opening connection to " + args[0] + "...");
                     URLConnection urlC = url.openConnection();
                     // Copy resource to local file, use remote file
                     // if no local file name specified
                     InputStream is = url.openStream();
                     // Print info about resource
                     System.out.print("Copying resource (type: " +
                     urlC.getContentType());
                     Date date=new Date(urlC.getLastModified());
                     System.out.println(", modified on: " + date.toLocaleString() + ")...");
                     System.out.flush();
                     FileOutputStream fos=null;                 if (args.length < 2)
                     {
                           String localFile=null;
                           // Get only file name
                           StringTokenizer st=new StringTokenizer(url.getFile(), "/");
                           
                           while (st.hasMoreTokens())
                                 localFile=st.nextToken();
                           fos = new FileOutputStream(localFile);
                     }
                     else
                           fos = new FileOutputStream(args[1]);                 int oneChar, count=0;
                     while ((oneChar=is.read()) != -1)
                     {
                           fos.write(oneChar);
                                         count++;
                     }                 is.close();
                     fos.close();
                     System.out.println(count + " byte(s) copied");
               }
               catch (MalformedURLException e)
               { 
                     System.err.println(e.toString()); 
               }
               catch (IOException e)
               { 
                     System.err.println(e.toString()); 
               }
         }
    }