|
@@ -8,10 +8,16 @@ import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
|
import java.net.HttpURLConnection;
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.MalformedURLException;
|
|
import java.net.MalformedURLException;
|
|
|
|
|
+import java.net.URI;
|
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
|
import java.net.URLConnection;
|
|
import java.net.URLConnection;
|
|
|
|
|
+import java.nio.charset.Charset;
|
|
|
|
|
+import java.util.Enumeration;
|
|
|
import java.util.List;
|
|
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;
|
|
import com.leanwo.management.model.ApplicationSetting;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -20,7 +26,7 @@ import com.leanwo.management.model.ApplicationSetting;
|
|
|
* @author YangZhiJie
|
|
* @author YangZhiJie
|
|
|
*/
|
|
*/
|
|
|
public class AutoUpdateService {
|
|
public class AutoUpdateService {
|
|
|
-
|
|
|
|
|
|
|
+ private static Logger logger = Logger.getLogger(AutoUpdateService.class);
|
|
|
/**
|
|
/**
|
|
|
* 更新的网站.
|
|
* 更新的网站.
|
|
|
*/
|
|
*/
|
|
@@ -92,27 +98,36 @@ public class AutoUpdateService {
|
|
|
private static Boolean existHttpPath(String httpPath){
|
|
private static Boolean existHttpPath(String httpPath){
|
|
|
URL httpurl = null;
|
|
URL httpurl = null;
|
|
|
try {
|
|
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) {
|
|
} catch (Exception e) {
|
|
|
|
|
+ logger.debug(httpurl + "文件不存在");
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 确定执行更新.
|
|
* 确定执行更新.
|
|
|
*/
|
|
*/
|
|
|
public void autoUpdate(ApplicationSetting setting) {
|
|
public void autoUpdate(ApplicationSetting setting) {
|
|
|
|
|
+ //需要更新的文件
|
|
|
List<String> autoUpdateFileNames = setting.getAutoUpdateFileNames();
|
|
List<String> autoUpdateFileNames = setting.getAutoUpdateFileNames();
|
|
|
String savePath = setting.getInstallPath();
|
|
String savePath = setting.getInstallPath();
|
|
|
|
|
+ //用于保存下载文件的目录
|
|
|
|
|
+ File saveDir = new File(savePath);
|
|
|
String fileName = savePath.substring(savePath.lastIndexOf("\\")+1);
|
|
String fileName = savePath.substring(savePath.lastIndexOf("\\")+1);
|
|
|
URL url;
|
|
URL url;
|
|
|
InputStream inputStream = null;
|
|
InputStream inputStream = null;
|
|
|
FileOutputStream fos = null;
|
|
FileOutputStream fos = null;
|
|
|
try {
|
|
try {
|
|
|
for (String s : autoUpdateFileNames) {
|
|
for (String s : autoUpdateFileNames) {
|
|
|
|
|
+ //url请求路径
|
|
|
String requestPath = updateSite + "/" + fileName + "/" + s;
|
|
String requestPath = updateSite + "/" + fileName + "/" + s;
|
|
|
if (!existHttpPath(requestPath)) {
|
|
if (!existHttpPath(requestPath)) {
|
|
|
throw new RuntimeException("需要更新的文件"+ requestPath + "不存在");
|
|
throw new RuntimeException("需要更新的文件"+ requestPath + "不存在");
|
|
@@ -123,27 +138,32 @@ public class AutoUpdateService {
|
|
|
conn.setConnectTimeout(5*1000);
|
|
conn.setConnectTimeout(5*1000);
|
|
|
//防止屏蔽程序抓取而返回403错误
|
|
//防止屏蔽程序抓取而返回403错误
|
|
|
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
|
|
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
|
|
|
-
|
|
|
|
|
//得到输入流
|
|
//得到输入流
|
|
|
inputStream = conn.getInputStream();
|
|
inputStream = conn.getInputStream();
|
|
|
- //获取自己数组
|
|
|
|
|
|
|
+ //获取自己的输入流数组
|
|
|
byte[] getData = readInputStream(inputStream);
|
|
byte[] getData = readInputStream(inputStream);
|
|
|
|
|
|
|
|
- //文件保存位置
|
|
|
|
|
- File saveDir = new File(savePath);
|
|
|
|
|
|
|
+ //判断要保存的目录是否存在,不存在就创建
|
|
|
if(!saveDir.exists()){
|
|
if(!saveDir.exists()){
|
|
|
saveDir.mkdir();
|
|
saveDir.mkdir();
|
|
|
}
|
|
}
|
|
|
File file = new File(saveDir + "/" + s);
|
|
File file = new File(saveDir + "/" + s);
|
|
|
fos = new FileOutputStream(file);
|
|
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) {
|
|
} catch (MalformedURLException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
} catch (FileNotFoundException e) {
|
|
} catch (FileNotFoundException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
} finally {
|
|
} finally {
|
|
|
if(fos!=null){
|
|
if(fos!=null){
|
|
|
try {
|
|
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();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|