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; } }