javascript有一个方法,你可以去那里查

解决方案 »

  1.   

    <%@ page import="java.text.*"%>
    <%@ page import="java.util.*"%>
    <%@ page import="java.io.*"%>
    <%@ page import="java.net.*"%>
    <%
     URL stdURL = null;
     BufferedReader stdIn = null;
     PrintWriter stdOut = null;
     try {
       stdURL = new URL("http://www.163.com");
     }
     catch (MalformedURLException e) {
       throw e;
     } try {
       stdIn = new BufferedReader(new InputStreamReader(stdURL.openStream()));
       stdOut = new PrintWriter(new BufferedWriter(new FileWriter("c:/163.html")));
     }
     catch (IOException e) {
     } /***把URL指定的页面以流的形式读出,写成指定的文件***/
     try {
       String strHtml = "";
       while((strHtml = stdIn.readLine())!=null) {
         stdOut.println(strHtml);
       }
     }
     catch (IOException e) {
       throw e;
     }
     finally {
       try {
         if(stdIn != null)
           stdIn.close();
         if(stdOut != null)
           stdOut.close();
       }
       catch (Exception e) {
         System.out.println(e);
       }
     }
    %>
      

  2.   

    public boolean downLoadFile2(String fileurl, String savepath)
    {//Things need to do:
    //Set connection timeout
    //Try more times to download
    //Catch a general Exception is not so good
    //...
    try
    {
    int httpStatusCode;
    URL url =new URL(fileurl);
    URLConnection conn = url.openConnection();
    conn.connect();
    HttpURLConnection httpconn =(HttpURLConnection)conn;
    httpStatusCode =httpconn.getResponseCode();
    if(httpStatusCode!=HttpURLConnection.HTTP_OK)
    {//HttpURLConnection return an error code
    System.out.println("Connect to "+fileurl+" failed,return code:"+httpStatusCode);
    return false;
    }
    int filelen = conn.getContentLength();
    InputStream is = conn.getInputStream();
    byte[] tmpbuf=new byte[1024];
    File savefile =new File(savepath);
    if(!savefile.exists())
    savefile.createNewFile();
    FileOutputStream fos = new FileOutputStream(savefile);
    int readnum = 0;
    if(filelen<0){
    while(readnum>-1)
    {
    readnum = is.read(tmpbuf);
    if(readnum>0)
    fos.write(tmpbuf,0,readnum);
    }
    }
    else
    {
    int readcount =0;
    while(readcount<filelen&&readnum!=-1)
    {
    readnum=is.read(tmpbuf);
    if(readnum>0)
    {
    fos.write(tmpbuf,0,readnum);
    readcount =readcount +readnum;
    }
    }
    if(readcount<filelen)
    {
    System.out.println("download error");
    is.close();
    fos.close();
    savefile.delete();
    return false;
    }
    }
    fos.flush();
    fos.close();
    is.close();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    return false;
    }
    return true;
    }