YangZhiJie há 8 anos atrás
pai
commit
5c2c108a1b

+ 1 - 1
autoStart.bat

@@ -1,2 +1,2 @@
-CD D:\LeanwoProgram_2017\ProdogV7-ServerManagement\ServerManagement
+CD D:\LeanwoProgram_2018\OtherServer\ServerManagement
 D:

+ 3 - 1
config/applicationContext.xml

@@ -12,7 +12,7 @@
 	<context:component-scan base-package="com.leanwo" />
 
 	<bean id="autoUpdateService" class="org.leanwo.management.util.AutoUpdateService">
-		<property name="updateSite" value="http://192.168.1.200:8080/AutoUpdate"></property>
+		<property name="updateSite" value="http://192.168.1.200:8888/AutoUpdate"></property>
 	</bean>	
 	
 	
@@ -64,6 +64,8 @@
 		<property name="autoUpdateFileNames">
 			<list>
 				<value>report-server-0.0.1-SNAPSHOT.jar</value>
+				<value>apache-tomcat-9.0.2-windows-x64.zip</value>
+				<value>apache-tomcat-9.0.2-windows-x86.zip</value>
 			</list>
 		</property>
 		<property name="xmlConfigPath" >

+ 96 - 12
src/main/java/org/leanwo/management/util/AutoUpdateService.java

@@ -8,10 +8,16 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
+import java.net.URI;
 import java.net.URL;
 import java.net.URLConnection;
+import java.nio.charset.Charset;
+import java.util.Enumeration;
 import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
 
+import org.apache.log4j.Logger;
 import com.leanwo.management.model.ApplicationSetting;
 
 /**
@@ -20,7 +26,7 @@ import com.leanwo.management.model.ApplicationSetting;
  * @author YangZhiJie
  */
 public class AutoUpdateService {
-	
+	private static Logger logger = Logger.getLogger(AutoUpdateService.class);
 	/**
 	 * 更新的网站.
 	 */
@@ -92,27 +98,36 @@ public class AutoUpdateService {
 	 private static Boolean existHttpPath(String httpPath){
 		 URL httpurl = null;
 		 try {
-		 httpurl = new URL(httpPath);
-		 URLConnection rulConnection = httpurl.openConnection();
-		 rulConnection.getInputStream();
-		 return true;
+		 httpurl = new URL(new URI(httpPath).toASCIIString());
+		 URLConnection urlConnection = httpurl.openConnection();
+		// urlConnection.getInputStream();
+		Long TotalSize=Long.parseLong(urlConnection.getHeaderField("Content-Length"));  
+		    if (TotalSize <= 0){
+		     return false;
+		    }
+		    return true;
 		 } catch (Exception e) {
+			 logger.debug(httpurl + "文件不存在");
 		 return false;
 		 }
 		 }
-	
+	 
 	/**
 	 * 确定执行更新.
 	 */
 	public void autoUpdate(ApplicationSetting setting) {
+		//需要更新的文件
 		List<String> autoUpdateFileNames = setting.getAutoUpdateFileNames();
 		String savePath = setting.getInstallPath();
+		//用于保存下载文件的目录 
+		File saveDir = new File(savePath);  
 		String fileName = savePath.substring(savePath.lastIndexOf("\\")+1);
 		URL url;
 		InputStream inputStream = null;
 		FileOutputStream fos = null;
 		try {
 			for (String s : autoUpdateFileNames) {
+				//url请求路径
 				String requestPath =  updateSite + "/" + fileName + "/" + s;
 				if (!existHttpPath(requestPath)) {
 					throw new RuntimeException("需要更新的文件"+ requestPath + "不存在");
@@ -123,27 +138,32 @@ public class AutoUpdateService {
 				conn.setConnectTimeout(5*1000);  
 				//防止屏蔽程序抓取而返回403错误  
 				conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");  
-					
 				//得到输入流  
 				inputStream = conn.getInputStream();    
-				//获取自己数组  
+				//获取自己的输入流数组  
 				byte[] getData = readInputStream(inputStream);      
 					
-				//文件保存位置  
-				File saveDir = new File(savePath);  
+				//判断要保存的目录是否存在,不存在就创建
 				if(!saveDir.exists()){  
 					saveDir.mkdir();  
 				}  
 				File file = new File(saveDir + "/"  + s);      
 				fos = new FileOutputStream(file);  
-				fos.write(getData); 
+				fos.write(getData);
+				if (s.substring(s.lastIndexOf(".")+1).equals("zip")) {
+					//解压改文件file至目录下C:\\Users\\TianGuangHui\\Service\\Report_Server_20171218
+					unZipFiles(file,"C:\\Users\\TianGuangHui\\Service\\Report_Server_20171218\\");
+				}
 				}
+			//unzipFromLoc(savePath);
 			} catch (MalformedURLException e) {
 				e.printStackTrace();
 			} catch (FileNotFoundException e) {
 				e.printStackTrace();
 			} catch (IOException e) {
 				e.printStackTrace();
+			} catch (Exception e) {
+				e.printStackTrace();
 			} finally {
 				if(fos!=null){  
 					try {
@@ -160,7 +180,71 @@ public class AutoUpdateService {
 					}  
 				} 
 			}   
-		
 	}
 	
+	  /** 
+     * 解压文件到指定目录 
+     * 解压后的文件名,和之前一致 
+     * @param zipFile   待解压的zip文件 
+     * @param descDir   指定目录 
+     */  
+    public static void unZipFiles(File zipFile, String descDir) {  
+          
+        ZipFile zip = null;
+        InputStream in = null;
+        FileOutputStream out = null;
+		try {
+			zip = new ZipFile(zipFile,Charset.forName("GBK"));//解决中文文件夹乱码  
+			
+			
+			String name = zip.getName().substring(zip.getName().lastIndexOf('\\')+1, zip.getName().lastIndexOf('.'));  
+			
+			File pathFile = new File(descDir+name);  
+			if (!pathFile.exists()) {  
+				pathFile.mkdirs();  
+			}  
+			
+			for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {  
+				ZipEntry entry = (ZipEntry) entries.nextElement();  
+				String zipEntryName = entry.getName();  
+				in = zip.getInputStream(entry);  
+				String outPath = (descDir + name +"/"+ zipEntryName).replaceAll("\\*", "/");  
+				// 判断路径是否存在,不存在则创建文件路径  
+				File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));  
+				if (!file.exists()) {  
+					file.mkdirs();  
+				}  
+				// 判断文件全路径是否为文件夹,如果是,不需要解压  
+				 if (new File(outPath).isDirectory()) {  
+                	continue;  
+            	} 
+				
+				out = new FileOutputStream(outPath);  
+				byte[] buf1 = new byte[1024];  
+				int len;  
+				while ((len = in.read(buf1)) > 0) {  
+					out.write(buf1, 0, len);  
+				}  
+			}  
+			logger.debug("解压完毕"); 
+		} catch (IOException e) {
+			e.printStackTrace();
+		}finally {
+			if (in != null){
+				try {
+					in.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+			if (out != null){
+				try {
+					in.close();
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+		}
+    }  
+	
 }