下載

 1.調用

URL url = new URL(DownStatic.murlsString); // 尋找資源服務器

connection = (HttpURLConnection) url.openConnection();  與服務器建立連接

InputStream is = connection.getInputStream();   建立資源流

randomAccessFile = new RandomAccessFile(DownStatic.mpathString, "rwd"); 建立本地文件

  1. public class DownStatic { 
  2.  
  3.     public static String murlsString; 
  4.     public static String mpathString; 
  5.     private static int compeleteSize; // 完成大小 
  6.  
  7.     public static native void displayProgress(String numString); 
  8.  
  9.     public static native void setFileSize(int pSize); 
  10.  
  11.     public static native void downloadFail(); 
  12.  
  13.     public static String downloadAndroid(String pPathString, String pUrlsString) { 
  14.         System.out.println("sendWeibo  :  " + pUrlsString); 
  15.         System.out.println("sendWeibo  :  " + pPathString); 
  16.  
  17.         File fileM = new File(pPathString); 
  18.         if (fileM.exists()) {// 如果存在刪除從新下載 
  19.             fileM.delete(); 
  20.         } 
  21.         DownStatic.murlsString = pUrlsString; 
  22.         DownStatic.mpathString = pPathString; 
  23.         DownStatic.down(); 
  24.  
  25.         return ""
  26.     } 
  27.  
  28.     public static void down() { 
  29.         HttpURLConnection connection = null
  30.         RandomAccessFile randomAccessFile = null
  31.         InputStream is = null
  32.  
  33.         try { 
  34.             URL url = new URL(DownStatic.murlsString); 
  35.             connection = (HttpURLConnection) url.openConnection(); 
  36.             connection.setConnectTimeout(5000); // 5秒 
  37.             connection.setRequestMethod("GET"); 
  38.             DownStatic.setFileSize(connection.getContentLength()); 
  39.  
  40.             // 設置範圍,格式爲Range:bytes x-y; 
  41.             // connection.setRequestProperty("Range", "bytes=" 
  42.             // + (0 + 0) + "-" + endPos); 
  43.             // randomAccessFile = new RandomAccessFile(localfile, "rwd"); // rwd 
  44.  
  45.             randomAccessFile = new RandomAccessFile(DownStatic.mpathString, 
  46.                     "rwd"); 
  47.  
  48.             // 數據同步讀寫 
  49.             // randomAccessFile.seek(startPos + compeleteSize); 
  50.             // 將要下載的文件寫到保存在保存路徑下的文件中 
  51.  
  52.             is = connection.getInputStream(); 
  53.  
  54.             byte[] buffer = new byte[4096]; // 每次寫入字節數 
  55.             int length = -1
  56.             String string; 
  57.  
  58.             // 不斷下載 
  59.             while ((length = is.read(buffer)) != -1) { 
  60.                 randomAccessFile.write(buffer, 0, length); 
  61.                 DownStatic.compeleteSize += length; 
  62.                 string = String.valueOf(DownStatic.compeleteSize); 
  63.                 DownStatic.displayProgress(string); 
  64.             } 
  65.  
  66.         } catch (Exception e) {// 下載失敗 
  67.             System.out.println("Exception e  Exception e"); 
  68.             DownStatic.downloadFail(); 
  69.             e.printStackTrace(); 
  70.         } finally { 
  71.             try { 
  72.                 is.close(); 
  73.                 randomAccessFile.close(); 
  74.                 connection.disconnect(); 
  75.             } catch (Exception e) { 
  76.                 System.out.println("finally finally e  Exception e"); 
  77.                 e.printStackTrace(); 
  78.             } 
  79.         } 
  80.  
  81.     } 
  82.  

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章