無為

          無為則可為,無為則至深!

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            190 Posts :: 291 Stories :: 258 Comments :: 0 Trackbacks
          我剛學java不久,寫了這個程序,小試身手各位有什么改進的地方,請賜教,

          import java.net.*;
          import java.io.*;
          import java.lang.*;

          public class down {
              public static void main(String[] args) throws Exception {
                  if(args.length==0){
                          System.out.println("Usage: down <url>");
                          System.exit(1);
                  }
                  URL yahoo = new URL(args[0]);
                  String name = args[0].substring(args[0].lastIndexOf("/")+1); 
                  HttpURLConnection connection = (HttpURLConnection)yahoo.openConnection(); ;

                  
                  File writeF = new File(name);
                  RandomAccessFile file=new RandomAccessFile(writeF,"rw");
                  
                  byte[] b = new byte[1024];
                  int nRead;
                  long down_length;
                  if(writeF.exists()){
                      down_length = writeF.length();
                      file.seek(down_length);
                  }else{
                      down_length = 0;
                  }   
                  connection.setRequestProperty("User-Agent","NetFox");
                  connection.setRequestProperty("RANGE","bytes="+down_length+"-");
                  DataInputStream in = new DataInputStream(connection.getInputStream());
                  String strLength = connection.getHeaderField("Content-Length");
                  long length = Long.parseLong(strLength);
                  System.out.println(name +" length: "+strLength);
                  if(connection.getHeaderField("Content-Range") != null )
                      System.out.println("Download :"+connection.getHeaderField("Content-Range")); 
                  
                  int down=0;
                  int flag =0;    
                  System.out.print(down_length/1024+"K\t->");
                  while ((nRead=in.read(b,0,b.length)) > 0){
                      file.write(b,0,nRead);
                      down = down +nRead;
                      if(down >= 1024){
                          down = down -1024;
                          flag++;
                          System.out.print(".");
                          if(flag%10 == 0) System.out.print(" ");
                          if(flag%50 == 0) {
                                down_length = down_length+1024*50;
                                int per = (int)(((double)down_length/(double)length)*100);
                                System.out.println("["+per+"%]");
                                System.out.print(down_length/1024+"K\t->");
                          }
                      }               
                      //in.flush();
                  }
                  System.out.println("\nFinished!");
                  in.close();
                  file.close();
              }
          }


          凡是有該標志的文章,都是該blog博主Caoer(草兒)原創,凡是索引、收藏
          、轉載請注明來處和原文作者。非常感謝。

          posted on 2005-12-14 13:08 草兒 閱讀(552) 評論(0)  編輯  收藏 所屬分類: Java編程經驗談
          主站蜘蛛池模板: 新安县| 惠来县| 新津县| 苏州市| 巩留县| 永仁县| 曲阳县| 定远县| 郑州市| 陇西县| 哈密市| 南安市| 三明市| 济南市| 临颍县| 浦县| 靖西县| 古丈县| 延津县| 谢通门县| 全椒县| 临泽县| 沙洋县| 庆安县| 玛曲县| 平和县| 台北市| 兴业县| 寿阳县| 苍梧县| 固阳县| 剑河县| 阿拉善左旗| 南和县| 汝州市| 怀集县| 隆子县| 宾川县| 兴海县| 容城县| 宜兰县|