PhoneGap文件下载

Downloader:

package com.phonegap.Sample;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONException;

import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;

public class Downloader extends Plugin
{

	@Override
	public PluginResult execute(String arg0, JSONArray arg1, String arg2)
	{
		if(arg0.equals("downloadFile"))
		{
			try
			{
				return this.downloadUrl(arg1.getString(0),arg1.getString(1),arg1.getString(2),arg1.getString(3));
				
			}
			catch (JSONException e)
			{
				e.printStackTrace();
				return new PluginResult(PluginResult.Status.ERROR,"param errors");
			}
		}else
		{
			return new PluginResult(PluginResult.Status.INVALID_ACTION);
		}
		
	}
	
	private PluginResult downloadUrl(String fileUrl, String dirName, String fileName,String overWirte)
	{
		File dir = new File(dirName);
		if(!dir.exists())
		{
			dir.mkdirs();
		}
		
		File file = new File("mnt/sdcard/" + dirName,fileName);
		if(overWirte.equals("false") && file.exists())
		{
			return new PluginResult(PluginResult.Status.OK,"exist");
		}
		
		try
		{
			URL url = new URL(fileUrl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setDoOutput(true);
			conn.connect();
			InputStream is = conn.getInputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			FileOutputStream fos = new FileOutputStream(file);
			while((len = is.read(buffer))!=-1)
			{
				fos.write(buffer,0,len);
			}
			fos.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return new PluginResult(PluginResult.Status.ERROR,"error : " + e);
		}
		return new PluginResult(PluginResult.Status.OK,fileName);
	}
	
	
}

downloader.js:

function Downloader(){	
	
}

Downloader.prototype.downloadFile = function(fileUrl,dirName,fileName,overwrite,win,fail) {
 if(overwrite==false) overwrite="false";
 else overwrite="true";
 PhoneGap.exec(win, fail, "Downloader", "downloadFile", [fileUrl,dirName,fileName,overwrite]);
 
};
 
PhoneGap.addConstructor(function() {
 PhoneGap.addPlugin("downloader", new Downloader());
 PluginManager.addService("Downloader","com.phonegap.Sample.Downloader");
});

html:

<!DOCTYPE html>

<head>
<meta charset=utf-8 />
<title>无标题文档</title>
<script type="text/javascript" src="phonegap.0.9.4.js"></script>
<script type="text/javascript" src="downloader.js"></script>
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<link rel="stylesheet" href="jquery.mobile-1.3.0.min.css" />
<script type="text/javascript" src="jquery.mobile-1.3.0.min.js"></script>
<script type="text/javascript">
	$(function(){
	$("#btn").bind("click",function(){
		
		window.plugins.downloader.downloadFile("http://127.0.0.1:8090/test/1.jpg","cache/","2.jpg",false,function(data){
				if(data == "exist"){
						alert("File already exist");
					}else
					{
						alert("File saved on sd card")
					}
			
			},function(data){
				
				alert("error: "+data);
				
				});
		});

});
</script>
</head>

<body>
	<a data-role="button" id="btn">下载</a>

</body>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值