首页 > 代码库 > 比较两个版本号新版本
比较两个版本号新版本
/**
* 版本信息
*
* @类名 :Meichat_version
* @作者 :kevin
* @创建时间 :2016-10-2下午8:25:13
*/
public class Meichat_version extends BasicServlet {
private static final long serialVersionUID = -7537429324065779134L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String currentVersion = "";
String model = "";
if (null != request.getParameter("currentVersion"))
currentVersion = request.getParameter("currentVersion");
if (null != request.getParameter("model"))
model = request.getParameter("model");
MongoDBDao dao = MongoDBDao.getMongoDBDaoInstance();
BasicDBObject query = new BasicDBObject();
query.put("name", "app");
query.put("number", "v" + currentVersion);
query.put("model", model);
JSONArray jar = dao.find(query, "yl_version");
StringBuffer sb = new StringBuffer();
sb = getStringbBuffer(jar, currentVersion);
String str = getJsonAppend(sb.toString(), MsgCode.MES_0, -1);
writeTo(response, str);
}
private StringBuffer getStringbBuffer(JSONArray jar, String version1) {
String flag = "N";// 无需强制升级
String dep = "";
String status = "0";
String time = "";
String number = "";
String verify = "";
StringBuffer sb = new StringBuffer();
JSONObject returnjsob = new JSONObject();
if (jar.size() > 0) {
JSONObject jsob = jar.getJSONObject(0);
long ctime = jsob.getLong("time");
MongoDBDao dao = MongoDBDao.getMongoDBDaoInstance();
JSONArray jarVer = dao.find(new BasicDBObject("time",
new BasicDBObject("$gte", ctime)), new BasicDBObject(
"time", 1), "yl_version", 1000);// -1降序,1升序
// 判断当前版本至最新版本之间是否存在强制升级的版本
for (int i = 0; i < jarVer.size(); i++) {
JSONObject jsobver = jarVer.getJSONObject(i);
jsobver.getString("number").replace("v", "");
if ("2".equals(jsobver.getString("status"))) {
JSONObject maxObj = jarVer.getJSONObject(jarVer.size() - 1);
status = "2";
dep = get(maxObj, "dep");
time = get(maxObj, "time");
number = get(maxObj, "number");
verify = get(maxObj, "verify");
flag = "Y";
break;
}
}
// 判断是否需要升级
if ("N".equals(flag)) {
JSONObject maxObj = jarVer.getJSONObject(jarVer.size() - 1);
String version2 = maxObj.getString("number").replace("v", "");
// 判断当前版本是否为最新版
int intValue = http://www.mamicode.com/compareVersion(version1, version2);
if (intValue >= 0)
status = "0";
else
status = "1";
dep = get(maxObj, "dep");
time = get(maxObj, "time");
number = get(maxObj, "number");
verify = get(maxObj, "verify");
}
}
returnjsob.put("dep", dep);
returnjsob.put("status", status);
returnjsob.put("time", time);
returnjsob.put("number", number);
returnjsob.put("verify", verify);
returnjsob.put("url", MongoUtil.getValueByKey("download_url"));
sb.append("\"version\":").append(returnjsob).append(",");
return sb;
}
/**
* 比较两个版本的大小
*
* @param version1
* @param version2
* @return
*/
public int compareVersion(String version1, String version2) {
if (version1.equals(version2)) {
return 0;
}
String[] version1Array = version1.split("\\.");
String[] version2Array = version2.split("\\.");
int index = 0;
// 获取最小长度值
int minLen = Math.min(version1Array.length, version2Array.length);
int diff = 0;
// 循环判断每位的大小
while (index < minLen
&& (diff = Integer.parseInt(version1Array[index])
- Integer.parseInt(version2Array[index])) == 0) {
index++;
}
if (diff == 0) {
// 如果位数不一致,比较多余位数,index那个大就进入那个for循环
for (int i = index; i < version1Array.length; i++) {
if (Integer.parseInt(version1Array[i]) > 0) {
return 1;
}
}
for (int i = index; i < version2Array.length; i++) {
if (Integer.parseInt(version2Array[i]) > 0) {
return -1;
}
}
return 0;
} else {
return diff > 0 ? 1 : -1;
}
}
}
比较两个版本号新版本