从服务器上下载apk

    public class DownLoadApk {
public static final String GET_URL = "http://10.125.159.220/api/builds/latest";

public static String readContentFromGet() throws IOException {
	String getURL = GET_URL;
	String laststr = "";
	URL getUrl = new URL(getURL);
	HttpURLConnection connection = (HttpURLConnection) getUrl
			.openConnection();
	connection.connect();
	BufferedReader reader = new BufferedReader(new InputStreamReader(
			connection.getInputStream(), "utf-8"));
	String lines;
	while ((lines = reader.readLine()) != null) {
		// lines = new String(lines.getBytes(), "utf-8");
		laststr += lines;
		laststr = laststr.substring(11, laststr.length() - 1);
	}
	reader.close();
	connection.disconnect();
	// System.out.println("Contents of get request ends");
	return laststr;
    }

        public static String getApkUrl(ApkInfo apkInfo) throws IOException {
	String downUrl = "";
	String[] url = getAllUrl();
	String[] tem = new String[url.length];

	int j = 0;
	for (int i = 0; i < url.length; i++) {
		if ((url[i].indexOf(apkInfo.getName()) != -1)
				&& (url[i].indexOf(apkInfo.getVersion()) != -1)
				&& (url[i].indexOf(apkInfo.getPaltform()) != -1)
				&& (url[i].indexOf(apkInfo.getKey()) != -1)
				&& (url[i].indexOf(apkInfo.getDate()) != -1)) {
			if (url[i] != null) {
				tem[j] = url[i];
				j++;
			}
		        }

	        }
	        if (tem[1] != null) {
		for (int i = 0; i < tem.length; i++) {
			if (tem[i] != null) {
				System.err.println(tem[i]);
			}
		    }
		    System.out.println("please add more parameters**********");
	        } else {
		    downUrl = tem[0];
	        }
	    System.out.println(downUrl);
	    return downUrl;
    }

/*

  • read Json data to array */ public static String[] getAllUrl() throws IOException { String str = readContentFromGet(); JSONArray array = JSONArray.fromObject(str); JSONObject jsonObject; String[] url = new String[array.size()]; for (int i = 0; i < array.size(); i++) { jsonObject = array.getJSONObject(i); url[i] = jsonObject.getString("url"); } return url; }

         public static void downApk(ApkInfo apkInfo, String apkUrl, String downPath) {
     try {
     	int byteSum = 0;
     	int byteRead = 0;
     	int byteCount = 0;
     	URL url = new URL(apkUrl);
     	URLConnection conn = url.openConnection();
    
     	String path = url.getPath();
     	String fileName = path.substring(path.lastIndexOf("."));
     	fileName = apkInfo.getName() + apkInfo.getDate() + "v"
     			+ apkInfo.getVersion() + fileName;
    
     	byteCount = conn.getContentLength();
     	double len = Double.parseDouble(byteCount + "") / (1024 * 1024);
     	DecimalFormat df = new DecimalFormat("0.##");
     	System.out.println("文件大小:" + df.format(len) + "MB");
    
     	InputStream inStream = conn.getInputStream();
     	FileOutputStream fos = new FileOutputStream(downPath + fileName);
     	byte[] buffer = new byte[1204];
     	System.out.println("文件开始下载....");
     	while ((byteRead = inStream.read(buffer)) != -1) {
     		byteSum += byteRead;
     		// int num = byteSum * 100 / Integer.parseInt(byteCount + "");
     		// System.out.println("已下载" + num + "%....");
     		fos.write(buffer, 0, byteRead);
     	}
     	System.out.println("文件下载完成!");
         } catch (FileNotFoundException e) {
     	e.printStackTrace();
         } catch (IOException e) {
     	e.printStackTrace();
         }
     }
    
         public static String getCurrentDate() {
     String dateString = "";
     SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
     dateString = format.format(new java.util.Date());
     return dateString;
     }
    

    // public static void main(String[] args) throws IOException { // ApkInfo apkInfo = new ApkInfo(); // apkInfo.setName("LenovoMusic"); // apkInfo.setVersion("3.0"); // apkInfo.setPaltform("common"); // apkInfo.setKey("dv2"); // apkInfo.setDate(getCurrentDate()); // downApk(apkInfo, getApkUrl(apkInfo)); // } }

     public class ApkInfo {
     private String name;
     private String version;
     private String paltform;
     private String key;
     private String date;
    
     public String getDate() {
     return date;
     }
    
     public void setDate(String date) {
     this.date = date;
     }
    
     public String getName() {
     return name;
     }
    
     public void setName(String name) {
     this.name = name;
     }
    
     public String getVersion() {
     return version;
     }
    
     public void setVersion(String version) {
     this.version = version;
     }
    
     public String getPaltform() {
     return paltform;
     }
    
     public void setPaltform(String paltform) {
     this.paltform = paltform;
     }
    
     public String getKey() {
     return key;
     }
    
     public void setKey(String key) {
     this.key = key;
     }
     }
    

转载于:https://my.oschina.net/u/435726/blog/227465

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值