|  | @ -11,7 +11,9 @@ import org.apache.http.message.BasicNameValuePair;
 | 
	
		
			
				|  |  | import org.json.JSONObject;
 | 
	
		
			
				|  |  | import org.springframework.util.StringUtils;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.io.*;
 | 
	
		
			
				|  |  | import java.io.ByteArrayOutputStream;
 | 
	
		
			
				|  |  | import java.io.File;
 | 
	
		
			
				|  |  | import java.io.InputStream;
 | 
	
		
			
				|  |  | import java.text.SimpleDateFormat;
 | 
	
		
			
				|  |  | import java.util.*;
 | 
	
		
			
				|  |  | 
 | 
	
	
		
			
				|  | @ -28,6 +30,7 @@ public class UpdateThread implements Runnable {
 | 
	
		
			
				|  |  |     private String versionCode = "";
 | 
	
		
			
				|  |  |     private String downloadPath = "";
 | 
	
		
			
				|  |  |     private String token;
 | 
	
		
			
				|  |  |     private boolean isStart = false;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Override
 | 
	
		
			
				|  |  |     public void run() {
 | 
	
	
		
			
				|  | @ -48,15 +51,19 @@ public class UpdateThread implements Runnable {
 | 
	
		
			
				|  |  |                         if (!StringUtils.isEmpty(filePath)) {
 | 
	
		
			
				|  |  |                             //关闭服務
 | 
	
		
			
				|  |  |                             closeService();
 | 
	
		
			
				|  |  |                             //睡眠8秒
 | 
	
		
			
				|  |  |                             //解压
 | 
	
		
			
				|  |  |                             if (zipFile(filePath)) {
 | 
	
		
			
				|  |  |                                 //启动服务
 | 
	
		
			
				|  |  |                                 startService();
 | 
	
		
			
				|  |  |                                 //上传结果
 | 
	
		
			
				|  |  |                                 upLoagResult();
 | 
	
		
			
				|  |  |                                 //更新本地版本号
 | 
	
		
			
				|  |  |                                 updateVersion();
 | 
	
		
			
				|  |  |                                 //判断服务是否启动
 | 
	
		
			
				|  |  |                                 String message = "启动服務失败";
 | 
	
		
			
				|  |  |                                 if (isStart) {
 | 
	
		
			
				|  |  |                                     message = "启动服務成功";
 | 
	
		
			
				|  |  |                                 }
 | 
	
		
			
				|  |  |                                 //上传结果
 | 
	
		
			
				|  |  |                                 upLoagResult(message);
 | 
	
		
			
				|  |  |                             }
 | 
	
		
			
				|  |  |                         }
 | 
	
		
			
				|  |  |                     } else {
 | 
	
	
		
			
				|  | @ -87,7 +94,7 @@ public class UpdateThread implements Runnable {
 | 
	
		
			
				|  |  |         db.execute(sql);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private void upLoagResult() {
 | 
	
		
			
				|  |  |     private void upLoagResult(String isStart) {
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             net.sf.json.JSONObject jo = net.sf.json.JSONObject.fromObject(resultString);
 | 
	
		
			
				|  |  | //            Map<String, Object> params = new HashMap<String, Object>();
 | 
	
	
		
			
				|  | @ -102,6 +109,7 @@ public class UpdateThread implements Runnable {
 | 
	
		
			
				|  |  |             formParams.add(new BasicNameValuePair("versionName", versionName));
 | 
	
		
			
				|  |  |             formParams.add(new BasicNameValuePair("versionCode", versionCode));
 | 
	
		
			
				|  |  |             formParams.add(new BasicNameValuePair("access_token", token));
 | 
	
		
			
				|  |  |             formParams.add(new BasicNameValuePair("message", isStart));
 | 
	
		
			
				|  |  |             formParams.add(new BasicNameValuePair("updateDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
 | 
	
		
			
				|  |  |             formParams.add(new BasicNameValuePair("orgCode", orgCode));
 | 
	
		
			
				|  |  |             resultString = HttpsClientUtil.post(ThreadConfig.SERVICE_URL + ThreadConfig.UPDATE_THREAD_UPLOADRESULT, formParams, ThreadConfig.SERVICE_USERNAME, ThreadConfig.SERVICE_PASSWORD);
 | 
	
	
		
			
				|  | @ -144,27 +152,17 @@ public class UpdateThread implements Runnable {
 | 
	
		
			
				|  |  |             if (system.contains("windows")) {
 | 
	
		
			
				|  |  |                 LogUtil.info("关闭windows服务");
 | 
	
		
			
				|  |  |                 //windows
 | 
	
		
			
				|  |  |                 String root = System.getProperty("catalina.home").replace('\\', '/');
 | 
	
		
			
				|  |  |                 String installPath = root.substring(0, root.lastIndexOf('/') + 1);
 | 
	
		
			
				|  |  |                 String url = installPath + ThreadConfig.TOMCAT_SERVICENAME;
 | 
	
		
			
				|  |  |                 String[] commandStop = {url + "\\bin\\shutdown.bat"};
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 String[] envp = {"JAVA_HOME=" + installPath + "\\jdk1.8.0_45", "CATALINA_HOME=" + url};
 | 
	
		
			
				|  |  |                 File workdir = new File(url + "\\bin");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 LogUtil.info("关闭服务目录:" + commandStop[0].toString());
 | 
	
		
			
				|  |  |                 Process process = Runtime.getRuntime().exec(commandStop, envp, workdir);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 //取得命令结果的输出流
 | 
	
		
			
				|  |  |                 InputStream fis = process.getInputStream();
 | 
	
		
			
				|  |  |                 //用一个读输出流类去读
 | 
	
		
			
				|  |  |                 BufferedReader br = new BufferedReader(new InputStreamReader(fis));
 | 
	
		
			
				|  |  |                 String line = null;
 | 
	
		
			
				|  |  |                 //逐行读取输出到控制台
 | 
	
		
			
				|  |  |                 while ((line = br.readLine()) != null) {
 | 
	
		
			
				|  |  |                 String batPathStop = UpdateThread.class.getResource("/").getPath() + "config/stop.bat";
 | 
	
		
			
				|  |  |                 Process psStop = Runtime.getRuntime().exec(batPathStop);
 | 
	
		
			
				|  |  |                 ByteArrayOutputStream baosStop = new ByteArrayOutputStream();
 | 
	
		
			
				|  |  |                 InputStream osStop = psStop.getInputStream();
 | 
	
		
			
				|  |  |                 byte bStop[] = new byte[256];
 | 
	
		
			
				|  |  |                 while (osStop.read(bStop) > 0) {
 | 
	
		
			
				|  |  |                     baosStop.write(bStop);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 fis.close();
 | 
	
		
			
				|  |  |                 br.close();
 | 
	
		
			
				|  |  |                 String sStop = baosStop.toString();
 | 
	
		
			
				|  |  |                 osStop.close();
 | 
	
		
			
				|  |  |                 baosStop.close();
 | 
	
		
			
				|  |  |                 //判断是否服务是否关闭
 | 
	
		
			
				|  |  |                 while (true) {
 | 
	
		
			
				|  |  |                     String batPath = UpdateThread.class.getResource("/").getPath() + "config/serviceAlive.bat";
 | 
	
	
		
			
				|  | @ -172,23 +170,24 @@ public class UpdateThread implements Runnable {
 | 
	
		
			
				|  |  |                     ByteArrayOutputStream baos = new ByteArrayOutputStream();
 | 
	
		
			
				|  |  |                     InputStream os = ps.getInputStream();
 | 
	
		
			
				|  |  |                     byte b[] = new byte[256];
 | 
	
		
			
				|  |  |                     while (os.read(b) > 0)
 | 
	
		
			
				|  |  |                     while (os.read(b) > 0) {
 | 
	
		
			
				|  |  |                         baos.write(b);
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     String s = baos.toString();
 | 
	
		
			
				|  |  |                     System.out.println(s);
 | 
	
		
			
				|  |  |                     os.close();
 | 
	
		
			
				|  |  |                     baos.close();
 | 
	
		
			
				|  |  |                     if (Boolean.valueOf(s.trim())) {
 | 
	
		
			
				|  |  |                     if (!Boolean.valueOf(s.trim())) {
 | 
	
		
			
				|  |  |                         isStart = true;
 | 
	
		
			
				|  |  |                         break;
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     Thread.sleep(800L);
 | 
	
		
			
				|  |  |                     LogUtil.info("服务正在关闭");
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 Thread.sleep(1000L);
 | 
	
		
			
				|  |  |             } else {
 | 
	
		
			
				|  |  |                 LogUtil.info("重启linux服务");
 | 
	
		
			
				|  |  |                 Runtime.getRuntime().exec("service " + ThreadConfig.TOMCAT_SERVICENAME + " stop");
 | 
	
		
			
				|  |  |                 Thread.sleep(6000L);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             LogUtil.info("关闭成功");
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             LogUtil.error("关闭失败:" + e.getMessage());
 | 
	
	
		
			
				|  | @ -204,27 +203,37 @@ public class UpdateThread implements Runnable {
 | 
	
		
			
				|  |  |             if (system.contains("windows")) {
 | 
	
		
			
				|  |  |                 LogUtil.info("启动windows服务");
 | 
	
		
			
				|  |  |                 //windows
 | 
	
		
			
				|  |  |                 String root = System.getProperty("catalina.home").replace('\\', '/');
 | 
	
		
			
				|  |  |                 String installPath = root.substring(0, root.lastIndexOf('/') + 1);
 | 
	
		
			
				|  |  |                 String url = installPath + ThreadConfig.TOMCAT_SERVICENAME;
 | 
	
		
			
				|  |  |                 String[] commandStart = {url + "\\bin\\startup.bat"};
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 String[] envp = {"JAVA_HOME=" + installPath + "\\jdk1.8.0_45", "CATALINA_HOME=" + url};
 | 
	
		
			
				|  |  |                 File workdir = new File(url + "\\bin");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 LogUtil.info("启动服务目录:" + commandStart[0].toString());
 | 
	
		
			
				|  |  |                 Process process = Runtime.getRuntime().exec(commandStart, envp, workdir);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 //取得命令结果的输出流
 | 
	
		
			
				|  |  |                 InputStream fis = process.getInputStream();
 | 
	
		
			
				|  |  |                 //用一个读输出流类去读
 | 
	
		
			
				|  |  |                 BufferedReader br = new BufferedReader(new InputStreamReader(fis));
 | 
	
		
			
				|  |  |                 String line = null;
 | 
	
		
			
				|  |  |                 //逐行读取输出到控制台
 | 
	
		
			
				|  |  |                 while ((line = br.readLine()) != null) {
 | 
	
		
			
				|  |  |                 String batPathStart = UpdateThread.class.getResource("/").getPath() + "config/start.bat";
 | 
	
		
			
				|  |  |                 Process psStart = Runtime.getRuntime().exec(batPathStart);
 | 
	
		
			
				|  |  |                 ByteArrayOutputStream baosStart = new ByteArrayOutputStream();
 | 
	
		
			
				|  |  |                 InputStream osStart = psStart.getInputStream();
 | 
	
		
			
				|  |  |                 byte bStart[] = new byte[256];
 | 
	
		
			
				|  |  |                 while (osStart.read(bStart) > 0) {
 | 
	
		
			
				|  |  |                     baosStart.write(bStart);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 osStart.close();
 | 
	
		
			
				|  |  |                 baosStart.close();
 | 
	
		
			
				|  |  |                 while (true) {
 | 
	
		
			
				|  |  |                     int i = 0;
 | 
	
		
			
				|  |  |                     String batPath = UpdateThread.class.getResource("/").getPath() + "config/serviceAlive.bat";
 | 
	
		
			
				|  |  |                     Process ps = Runtime.getRuntime().exec(batPath);
 | 
	
		
			
				|  |  |                     ByteArrayOutputStream baos = new ByteArrayOutputStream();
 | 
	
		
			
				|  |  |                     InputStream os = ps.getInputStream();
 | 
	
		
			
				|  |  |                     byte b[] = new byte[256];
 | 
	
		
			
				|  |  |                     while (os.read(b) > 0)
 | 
	
		
			
				|  |  |                         baos.write(b);
 | 
	
		
			
				|  |  |                     String s = baos.toString();
 | 
	
		
			
				|  |  |                     os.close();
 | 
	
		
			
				|  |  |                     baos.close();
 | 
	
		
			
				|  |  |                     if (Boolean.valueOf(s.trim())) {
 | 
	
		
			
				|  |  |                         i++;
 | 
	
		
			
				|  |  |                         if (i > 3) {
 | 
	
		
			
				|  |  |                             break;
 | 
	
		
			
				|  |  |                         }
 | 
	
		
			
				|  |  |                         LogUtil.info("第" + i + "次判断服务已经启动");
 | 
	
		
			
				|  |  |                         Thread.sleep(1000L);
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 fis.close();
 | 
	
		
			
				|  |  |                 br.close();
 | 
	
		
			
				|  |  |             } else {
 | 
	
		
			
				|  |  |                 LogUtil.info("启动linux服务");
 | 
	
		
			
				|  |  |                 Runtime.getRuntime().exec("service " + ThreadConfig.TOMCAT_SERVICENAME + " start");
 | 
	
	
		
			
				|  | @ -261,7 +270,6 @@ public class UpdateThread implements Runnable {
 | 
	
		
			
				|  |  |             //  params.put("orgCode", orgCode);
 | 
	
		
			
				|  |  |             // params.put("access_token", token);
 | 
	
		
			
				|  |  |             LogUtil.info("开始下载文件,文件下載地址在:" + ThreadConfig.SYSTEM_DOWNLOADPATH + downloadPath);
 | 
	
		
			
				|  |  |             System.out.println(ThreadConfig.SYSTEM_DOWNLOADPATH + downloadPath);
 | 
	
		
			
				|  |  |             // HttpClientUtil.downFile(path, new HashMap<String, Object>(), , "", "");
 | 
	
		
			
				|  |  |             HttpClientUtil.getFile(ThreadConfig.SYSTEM_DOWNLOADPATH + downloadPath, path);
 | 
	
		
			
				|  |  |             LogUtil.info("下载文件成功,文件保存目录:" + path);
 | 
	
	
		
			
				|  | @ -342,11 +350,6 @@ public class UpdateThread implements Runnable {
 | 
	
		
			
				|  |  |         return folderPath + fileName + ".war";
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public static void main(String[] args) throws Exception {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public static boolean getProcess() {
 | 
	
		
			
				|  |  |         boolean flag = false;
 | 
	
		
			
				|  |  |         try {
 | 
	
	
		
			
				|  | @ -357,16 +360,32 @@ public class UpdateThread implements Runnable {
 | 
	
		
			
				|  |  |             while (os.read(b) > 0)
 | 
	
		
			
				|  |  |                 baos.write(b);
 | 
	
		
			
				|  |  |             String s = baos.toString();
 | 
	
		
			
				|  |  |             System.out.println(s);
 | 
	
		
			
				|  |  |             if (s.indexOf("smss.exe") >= 0) {
 | 
	
		
			
				|  |  |                 System.out.println("yes ");
 | 
	
		
			
				|  |  |                 flag = true;
 | 
	
		
			
				|  |  |             } else {
 | 
	
		
			
				|  |  |                 System.out.println("no ");
 | 
	
		
			
				|  |  |                 flag = false;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         } catch (java.io.IOException ioe) {
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return flag;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public static void main(String[] args) throws Exception {
 | 
	
		
			
				|  |  |         while (true) {
 | 
	
		
			
				|  |  |             String batPath = UpdateThread.class.getResource("/").getPath() + "config/serviceAlive.bat";
 | 
	
		
			
				|  |  |             Process ps = Runtime.getRuntime().exec(batPath);
 | 
	
		
			
				|  |  |             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 | 
	
		
			
				|  |  |             InputStream os = ps.getInputStream();
 | 
	
		
			
				|  |  |             byte b[] = new byte[256];
 | 
	
		
			
				|  |  |             while (os.read(b) > 0)
 | 
	
		
			
				|  |  |                 baos.write(b);
 | 
	
		
			
				|  |  |             String s = baos.toString();
 | 
	
		
			
				|  |  |             System.out.println(s);
 | 
	
		
			
				|  |  |             os.close();
 | 
	
		
			
				|  |  |             baos.close();
 | 
	
		
			
				|  |  |             if (Boolean.valueOf(s.trim())) {
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |