android下载

  1. plugin:

  2. package com.phonegap.Sample;  
  3.   
  4. import java.io.File;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.net.HttpURLConnection;  
  8. import java.net.URL;  
  9.   
  10. import org.json.JSONArray;  
  11. import org.json.JSONException;  
  12.   
  13. import com.phonegap.api.Plugin;  
  14. import com.phonegap.api.PluginResult;  
  15.   
  16. public class Downloader extends Plugin  
  17. {  
  18.   
  19.     @Override  
  20.     public PluginResult execute(String arg0, JSONArray arg1, String arg2)  
  21.     {  
  22.         if(arg0.equals("downloadFile"))  
  23.         {  
  24.             try  
  25.             {  
  26.                 return this.downloadUrl(arg1.getString(0),arg1.getString(1),arg1.getString(2),arg1.getString(3));  
  27.                   
  28.             }  
  29.             catch (JSONException e)  
  30.             {  
  31.                 e.printStackTrace();  
  32.                 return new PluginResult(PluginResult.Status.ERROR,"param errors");  
  33.             }  
  34.         }else  
  35.         {  
  36.             return new PluginResult(PluginResult.Status.INVALID_ACTION);  
  37.         }  
  38.           
  39.     }  
  40.       
  41.     private PluginResult downloadUrl(String fileUrl, String dirName, String fileName,String overWirte)  
  42.     {  
  43.         File dir = new File(dirName);  
  44.         if(!dir.exists())  
  45.         {  
  46.             dir.mkdirs();  
  47.         }  
  48.           
  49.         File file = new File("mnt/sdcard/" + dirName,fileName);  
  50.         if(overWirte.equals("false") && file.exists())  
  51.         {  
  52.             return new PluginResult(PluginResult.Status.OK,"exist");  
  53.         }  
  54.           
  55.         try  
  56.         {  
  57.             URL url = new URL(fileUrl);  
  58.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  59.             conn.setRequestMethod("GET");  
  60.             conn.setDoOutput(true);  
  61.             conn.connect();  
  62.             InputStream is = conn.getInputStream();  
  63.             byte[] buffer = new byte[1024];  
  64.             int len = 0;  
  65.             FileOutputStream fos = new FileOutputStream(file);  
  66.             while((len = is.read(buffer))!=-1)  
  67.             {  
  68.                 fos.write(buffer,0,len);  
  69.             }  
  70.             fos.close();  
  71.         }  
  72.         catch (Exception e)  
  73.         {  
  74.             e.printStackTrace();  
  75.             return new PluginResult(PluginResult.Status.ERROR,"error : " + e);  
  76.         }  
  77.         return new PluginResult(PluginResult.Status.OK,fileName);  
  78.     }  
  79.       
  80.       
  81. }  
  82. js:
    1. function Downloader(){    
    2.       
    3. }  
    4.   
    5. Downloader.prototype.downloadFile = function(fileUrl,dirName,fileName,overwrite,win,fail) {  
    6.  if(overwrite==false) overwrite="false";  
    7.  else overwrite="true";  
    8.  PhoneGap.exec(win, fail, "Downloader""downloadFile", [fileUrl,dirName,fileName,overwrite]);  
    9.    
    10. };  
    11.    
    12. PhoneGap.addConstructor(function() {  
    13.  PhoneGap.addPlugin("downloader"new Downloader());  
    14.  PluginManager.addService("Downloader","com.phonegap.Sample.Downloader");  
    15. });  
    html:
    1. <!DOCTYPE html>  
    2.   
    3. <head>  
    4. <meta charset=utf-8 />  
    5. <title>无标题文档</title>  
    6. <script type="text/javascript" src="phonegap.0.9.4.js"></script>  
    7. <script type="text/javascript" src="downloader.js"></script>  
    8. <script type="text/javascript" src="jquery-1.8.3.min.js"></script>  
    9. <link rel="stylesheet" href="jquery.mobile-1.3.0.min.css" />  
    10. <script type="text/javascript" src="jquery.mobile-1.3.0.min.js"></script>  
    11. <script type="text/javascript">  
    12.     $(function(){  
    13.     $("#btn").bind("click",function(){  
    14.           
    15.         window.plugins.downloader.downloadFile("http://127.0.0.1:8090/test/1.jpg","cache/","2.jpg",false,function(data){  
    16.                 if(data == "exist"){  
    17.                         alert("File already exist");  
    18.                     }else  
    19.                     {  
    20.                         alert("File saved on sd card")  
    21.                     }  
    22.               
    23.             },function(data){  
    24.                   
    25.                 alert("error: "+data);  
    26.                   
    27.                 });  
    28.         });  
    29.   
    30. });  
    31. </script>  
    32. </head>  
    33.   
    34. <body>  
    35.     <a data-role="button" id="btn">下载</a>  
    36.   
    37. </body>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值