下载

 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.  

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(下载)