AutoUpdateService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package com.leanwo.management.service;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.math.BigInteger;
  10. import java.net.HttpURLConnection;
  11. import java.net.MalformedURLException;
  12. import java.net.URI;
  13. import java.net.URL;
  14. import java.net.URLConnection;
  15. import java.nio.charset.Charset;
  16. import java.security.MessageDigest;
  17. import java.text.SimpleDateFormat;
  18. import java.util.Date;
  19. import java.util.Enumeration;
  20. import java.util.List;
  21. import java.util.zip.ZipEntry;
  22. import java.util.zip.ZipFile;
  23. import org.apache.log4j.Logger;
  24. import org.springframework.stereotype.Service;
  25. import com.leanwo.management.model.ApplicationSetting;
  26. /**
  27. * 自动更新服务.
  28. *
  29. * @author YangZhiJie
  30. */
  31. public class AutoUpdateService {
  32. private static Logger logger = Logger.getLogger(AutoUpdateService.class);
  33. /**
  34. * 更新的网站.
  35. */
  36. private String updateSite;
  37. /**
  38. * Gets the 更新的网站.
  39. *
  40. * @return the 更新的网站
  41. */
  42. public String getUpdateSite() {
  43. return updateSite;
  44. }
  45. /**
  46. * Sets the 更新的网站.
  47. *
  48. * @param updateSite the new 更新的网站
  49. */
  50. public void setUpdateSite(String updateSite) {
  51. this.updateSite = updateSite;
  52. }
  53. /**
  54. * 检查服务器上是否有可更新的程序
  55. * @return
  56. */
  57. public boolean canUpdate(ApplicationSetting setting) {
  58. List<String> autoUpdateFileNames = setting.getAutoUpdateFileNames();
  59. if (autoUpdateFileNames == null || autoUpdateFileNames.size() < 1) {
  60. return false;
  61. }
  62. return true;
  63. }
  64. /**
  65. * 从输入流中获取字节数组
  66. * @param inputStream
  67. * @return
  68. * @throws IOException
  69. */
  70. public static byte[] readInputStream(InputStream inputStream) {
  71. byte[] buffer = new byte[1024];
  72. int len = 0;
  73. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  74. try {
  75. while((len = inputStream.read(buffer)) != -1) {
  76. bos.write(buffer, 0, len);
  77. }
  78. } catch (IOException e) {
  79. e.printStackTrace();
  80. } finally {
  81. if(bos != null){
  82. try {
  83. bos.close();
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
  89. return bos.toByteArray();
  90. }
  91. /**
  92. * 判断文件是否存在
  93. * @param httpPath
  94. * @return
  95. */
  96. private static Boolean existHttpPath(String httpPath){
  97. URL httpurl = null;
  98. try {
  99. httpurl = new URL(new URI(httpPath).toASCIIString());
  100. URLConnection urlConnection = httpurl.openConnection();
  101. // urlConnection.getInputStream();
  102. Long TotalSize=Long.parseLong(urlConnection.getHeaderField("Content-Length"));
  103. if (TotalSize <= 0){
  104. return false;
  105. }
  106. return true;
  107. } catch (Exception e) {
  108. logger.debug(httpurl + "文件不存在");
  109. return false;
  110. }
  111. }
  112. /**
  113. * 确定执行更新.
  114. */
  115. public void autoUpdate(ApplicationSetting setting) {
  116. //需要更新的文件
  117. List<String> autoUpdateFileNames = setting.getAutoUpdateFileNames();
  118. String savePath = setting.getInstallPath();
  119. //用于保存下载文件的目录
  120. File saveDir = new File(savePath);
  121. File tempDir = new File(savePath + "/" + "temp");
  122. String fileName = savePath.substring(savePath.lastIndexOf("\\")+1);
  123. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  124. String dateStr = sdf.format(new Date());
  125. URL url;
  126. InputStream inputStream = null;
  127. FileOutputStream fos = null;
  128. try {
  129. for (String s : autoUpdateFileNames) {
  130. //url请求路径
  131. String requestPath = updateSite + "/" + fileName + "/" + s;
  132. //判断需要下载的文件是否存在
  133. if (!existHttpPath(requestPath)) {
  134. throw new RuntimeException("需要更新的文件"+ requestPath + "不存在");
  135. }
  136. //判断需要下载的文件对应的MD5文件是否存在
  137. String sd = s.substring(0 ,s.lastIndexOf(".")+1)+"md5";
  138. String md5Path = updateSite + "/" + fileName + "/" + sd;
  139. //判断需要下载的文件对应的MD5文件是否存在
  140. if (!existHttpPath(md5Path)) {
  141. throw new RuntimeException("需要更新的文件"+ requestPath + "的MD5文件不存在");
  142. }
  143. //读取远程md5文件中值
  144. url = new URL(md5Path);
  145. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  146. //设置超时间为5秒
  147. conn.setConnectTimeout(5*1000);
  148. //防止屏蔽程序抓取而返回403错误
  149. conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  150. //得到输入流
  151. inputStream = conn.getInputStream();
  152. //获取自己的输入流数组
  153. byte[] getData = readInputStream(inputStream);
  154. String md5Value = new String(getData);
  155. //判断要保存的目录是否存在,不存在就创建
  156. if(!saveDir.exists()){
  157. saveDir.mkdir();
  158. }
  159. File file = new File(saveDir + "/" + s);
  160. if (file.exists()){
  161. String fileMD5 = getFileMD5(file);
  162. if (fileMD5.equals(md5Value)){
  163. throw new RuntimeException("需要更新的文件"+ requestPath + "已经是最新的,暂时要不需要更新");
  164. }
  165. //如果元文件存在,且MD5值与远程不一样,移动原文件到old+time文件夹
  166. boolean move1 = moveFileToDir(file, savePath + "/old" + dateStr);
  167. if (!move1){
  168. throw new RuntimeException("移动文件"+ file.getAbsolutePath() + "到old时出错");
  169. }
  170. //copyFile(file, saveDir + "/old" + dateStr);
  171. }
  172. if(!tempDir.exists()){
  173. tempDir.mkdir();
  174. }
  175. //要更新的文件存在,并且改文件的MD5值与远程不一样
  176. //或者要更新文件在本地不存在,执行更新操作
  177. url = new URL(requestPath);
  178. conn = (HttpURLConnection)url.openConnection();
  179. //设置超时间为5秒
  180. conn.setConnectTimeout(5*1000);
  181. //防止屏蔽程序抓取而返回403错误
  182. conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  183. //得到输入流
  184. inputStream = conn.getInputStream();
  185. //获取自己的输入流数组
  186. getData = readInputStream(inputStream);
  187. fos = new FileOutputStream(new File(tempDir + "/" + s));
  188. fos.write(getData);
  189. inputStream.close();
  190. fos.close();
  191. String downloadFileMD5 = getFileMD5(new File(tempDir + "/" + s));
  192. if (!downloadFileMD5.equals(md5Value)){
  193. throw new RuntimeException("更新的文件"+ requestPath + "未下载完整,请重新更新");
  194. }
  195. //将temp中的新文件复制到指定目录并解压
  196. boolean move2 = moveFileToDir(new File(tempDir + "/" + s), savePath);
  197. if (!move2) {
  198. throw new RuntimeException("从temp移动文件"+ requestPath + "到指定目录出错");
  199. }
  200. //copyFile(new File(tempDir + "/" + s), savePath);
  201. if (s.substring(s.lastIndexOf(".")+1).equals("zip")) {
  202. //将更新后的文件解压
  203. unZipFiles(new File(saveDir + "//" + s), savePath+"\\");
  204. }
  205. }
  206. //unzipFromLoc(savePath);
  207. } catch (MalformedURLException e) {
  208. e.printStackTrace();
  209. } catch (FileNotFoundException e) {
  210. e.printStackTrace();
  211. } catch (IOException e) {
  212. e.printStackTrace();
  213. } catch (Exception e) {
  214. e.printStackTrace();
  215. } finally {
  216. if(fos!=null){
  217. try {
  218. fos.close();
  219. } catch (IOException e) {
  220. e.printStackTrace();
  221. }
  222. }
  223. if(inputStream!=null){
  224. try {
  225. inputStream.close();
  226. } catch (IOException e) {
  227. e.printStackTrace();
  228. }
  229. }
  230. }
  231. }
  232. /**
  233. * 解压文件到指定目录
  234. * 解压后的文件名,和之前一致
  235. * @param zipFile 待解压的zip文件
  236. * @param descDir 指定目录
  237. */
  238. public static void unZipFiles(File zipFile, String descDir) {
  239. ZipFile zip = null;
  240. InputStream in = null;
  241. FileOutputStream out = null;
  242. try {
  243. zip = new ZipFile(zipFile, Charset.forName("gbk"));
  244. String name = zip.getName().substring(zip.getName().lastIndexOf('\\')+1, zip.getName().lastIndexOf('.'));
  245. File pathFile = new File(descDir+name);
  246. //判断解压到的目录是否存在,存在则清空并删除
  247. if (pathFile.exists() && pathFile.isDirectory()) {
  248. deleteAllFile(pathFile);
  249. }
  250. //不存在就创建后解压
  251. pathFile.mkdirs();
  252. for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {
  253. ZipEntry entry = (ZipEntry) entries.nextElement();
  254. String zipEntryName = entry.getName();
  255. in = zip.getInputStream(entry);
  256. String outPath = (descDir + name +"/"+ zipEntryName).replaceAll("\\*", "/");
  257. // 判断路径是否存在,不存在则创建文件路径
  258. File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
  259. if (!file.exists()) {
  260. file.mkdirs();
  261. }
  262. // 判断文件全路径是否为文件夹,如果是,不需要解压
  263. if (new File(outPath).isDirectory()) {
  264. continue;
  265. }
  266. out = new FileOutputStream(outPath);
  267. byte[] buf1 = new byte[1024];
  268. int len;
  269. while ((len = in.read(buf1)) > 0) {
  270. out.write(buf1, 0, len);
  271. }
  272. }
  273. logger.debug("解压完毕");
  274. } catch (IOException e) {
  275. e.printStackTrace();
  276. }finally {
  277. if (in != null){
  278. try {
  279. in.close();
  280. } catch (IOException e) {
  281. e.printStackTrace();
  282. }
  283. }
  284. if (out != null){
  285. try {
  286. in.close();
  287. } catch (IOException e) {
  288. e.printStackTrace();
  289. }
  290. }
  291. }
  292. }
  293. /**
  294. * 清空并删除指定目录
  295. * @param filePath
  296. */
  297. public static void deleteAllFile(File file){
  298. File[] listFiles = file.listFiles();
  299. if (listFiles.length == 0){
  300. file.delete();
  301. }else{
  302. for (File f : listFiles) {
  303. if(f.isDirectory()){
  304. deleteAllFile(f);
  305. }else{
  306. f.delete();
  307. }
  308. }
  309. file.delete();
  310. }
  311. }
  312. /**
  313. * 读取指定文件的MD5值
  314. * @param file
  315. * @return
  316. */
  317. public static String getFileMD5(File file) {
  318. if (!file.isFile()) {
  319. return null;
  320. }
  321. MessageDigest digest = null;
  322. FileInputStream in = null;
  323. byte buffer[] = new byte[2014];
  324. int len;
  325. try {
  326. digest =MessageDigest.getInstance("MD5");
  327. in = new FileInputStream(file);
  328. while ((len = in.read(buffer)) != -1) {
  329. digest.update(buffer, 0, len);
  330. }
  331. BigInteger bigInt = new BigInteger(1, digest.digest());
  332. return bigInt.toString(16);
  333. } catch (Exception e) {
  334. e.printStackTrace();
  335. return null;
  336. } finally {
  337. if (in != null){
  338. try {
  339. in.close();
  340. } catch (Exception e) {
  341. e.printStackTrace();
  342. }
  343. }
  344. }
  345. }
  346. /**
  347. * 复制文件到指定文件夹下
  348. * @param oldPath
  349. * @param newPath
  350. * @throws IOException
  351. */
  352. public static void copyFile(File file, String newPath) {
  353. File newDir = new File(newPath);
  354. if (!newDir.exists()){
  355. newDir.mkdirs();
  356. }
  357. File newFile = new File(newPath + "/" + file.getName());
  358. FileInputStream in = null;
  359. FileOutputStream out = null;
  360. try {
  361. in = new FileInputStream(file);
  362. out = new FileOutputStream(newFile);;
  363. byte[] buffer=new byte[2097152];
  364. while((in.read(buffer)) != -1){
  365. out.write(buffer);
  366. }
  367. } catch (FileNotFoundException e) {
  368. e.printStackTrace();
  369. } catch (IOException e) {
  370. e.printStackTrace();
  371. }finally {
  372. if (in != null){
  373. try {
  374. in.close();
  375. } catch (IOException e) {
  376. e.printStackTrace();
  377. }
  378. }
  379. if (out != null){
  380. try {
  381. out.close();
  382. } catch (IOException e) {
  383. e.printStackTrace();
  384. }
  385. }
  386. }
  387. }
  388. /**
  389. * 移动指定文件到指定目录下
  390. */
  391. public static boolean moveFileToDir(File file, String dirPath){
  392. boolean flag;
  393. File saveDir = new File(dirPath);
  394. if (!saveDir.exists()){
  395. saveDir.mkdirs();
  396. }
  397. flag = file.renameTo(new File(saveDir+"/"+file.getName()));
  398. return flag;
  399. }
  400. }