public static void getNetFile(String url, String fileName) {
      try{
         InputStream  i =  new java.net.URL(url).openStream();
         OutputStream o = new FileOutputStream(fileName);
         byte[] buf = new byte[2000];
         int len;
         int count = 0;
         while((len = i.read(buf)) != -1) {
            count +=len;
            System.out.println(count);
            o.write(buf,0,len);
         }         i.close();o.close();
         }catch(Exception e){ e.printStackTrace();}
   }

解决方案 »

  1.   

    import java.net.*;
    而且你的InputStream和OutputStream没有必要都要!
      

  2.   

    请问大峡,如果这个url带有参数且要同时提交cookie或者session时又如何下载呢
    (cookie和session已知)比如:http://localhost:8080/download.jsp?name=aaa
      

  3.   

    你可能得用HttpURLConnectionHttpURLConnection uc0 = (HttpURLConnection)url.openConnection();
    其中对session的处理:
    String cookieVal = uc0.getHeaderField("Set-Cookie");
    if(cookieVal != null)
    {
    sessionId = cookieVal.substring(0, cookieVal.indexOf(";"));//将cookie记下来
    }之后:uc1.setRequestProperty("Cookie", sessionId);