在增量更新项目时,做好备份十分重要,这里提供一个方法备份java Web所更新的文件。

把更新包放在指定目录,配好如下webappFolder、updateFolder以及bakeupFolder的路径。

  1. import java.io.BufferedWriter;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.PrintStream;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Date;
  12.  
  13. public class MyBak
  14. {
  15. private static StringBuffer globalTxtMessage = new StringBuffer("");
  16. private static StringBuffer stdoutMessage = new StringBuffer("");
  17. private static int totalFileNumber = 0;
  18. private static int totalSuccessCopyNumber = 0;
  19. private static int totalNewFileNumber = 0;
  20.  
  21. public static void main(String[] args)
  22. {
  23. String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  24.  
  25. String webappFolder = "/www/htdocs/webapps/M4";
  26.  
  27. String updateFolder = "/home/whaty/bakeForUpdate/update/" + today;
  28.  
  29. String backupFolder = "/home/whaty/bakeForUpdate/bake/" + today;
  30.  
  31. if (webappFolder.equals(backupFolder)) {
  32. stdoutMessage.append("you can't just copy thins to the webapps folder!\n");
  33. globalTxtMessage = new StringBuffer("");
  34. stdoutMessage = new StringBuffer("");
  35. } else {
  36. String msg = backupFiles(updateFolder, webappFolder, backupFolder);
  37. System.out.print(msg);
  38. globalTxtMessage = new StringBuffer("");
  39. stdoutMessage = new StringBuffer("");
  40. }
  41. }
  42.  
  43. public static String backupFiles(String _updateFolder, String _webappFolder, String _backupFolder)
  44. {
  45. String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  46. String now = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
  47. try {
  48. File updateFolder = new File(_updateFolder);
  49. File webappFolder = new File(_webappFolder);
  50. File backupFolder = new File(_backupFolder);
  51.  
  52. globalTxtMessage.append("============" + now + "Backup Log==========\n");
  53. globalTxtMessage.append("============初始化条件测试开始===============\n");
  54. stdoutMessage.append("============init test start===============\n");
  55.  
  56. if (!(updateFolder.exists())) {
  57. globalTxtMessage.append(_updateFolder + "更新包文件夹找不到!\n");
  58. stdoutMessage.append(" the upload folder \"" + _updateFolder + "\" can Not found! \n please make sure you have upload the folder!!!!\n");
  59. }
  60. if (!(webappFolder.exists())) {
  61. globalTxtMessage.append("项目Webapp文件夹" + _webappFolder + "找不到!\n");
  62. stdoutMessage.append("the Webapp folder " + _webappFolder + " can Not found!!!\n please make sure the folder is exists!!!!\n");
  63. return stdoutMessage.toString();
  64. }
  65. if (!(backupFolder.exists())) {
  66. globalTxtMessage.append("自动创建备份文件夹" + backupFolder.getAbsolutePath() + "\n");
  67. stdoutMessage.append("auto create folder " + backupFolder.getAbsolutePath() + "\n");
  68. backupFolder.mkdirs();
  69. }
  70. globalTxtMessage.append("============初始化条件测试结束===============\n");
  71. stdoutMessage.append("============init test end===============\n");
  72.  
  73. Long start = Long.valueOf(System.currentTimeMillis());
  74.  
  75. copyFilesToDirectory(updateFolder, webappFolder, backupFolder);
  76.  
  77. Long end = Long.valueOf(System.currentTimeMillis());
  78.  
  79. Long oprationMillisTime = Long.valueOf(end.longValue() - start.longValue());
  80. Long oprationSecondTime = Long.valueOf((end.longValue() - start.longValue()) / 1000L);
  81.  
  82. globalTxtMessage.append("备份完成!\n");
  83. globalTxtMessage.append("=========================================\n");
  84. globalTxtMessage.append("总文件数:" + totalFileNumber + "\n");
  85. globalTxtMessage.append("成功复制:" + totalSuccessCopyNumber + "\n");
  86. globalTxtMessage.append("新增文件数:" + totalNewFileNumber + "\n");
  87. globalTxtMessage.append("拷贝文件所需时间[oprationMillisTime]:" + oprationMillisTime + " 毫秒\n");
  88. globalTxtMessage.append("拷贝文件所需时间[oprationSecondTime]:" + oprationSecondTime + " 秒\n");
  89.  
  90. stdoutMessage.append("backup done!\n");
  91. stdoutMessage.append("total files number:" + totalFileNumber + "\n");
  92. stdoutMessage.append("success copies number:" + totalSuccessCopyNumber + "\n");
  93. stdoutMessage.append("new files number:" + totalNewFileNumber + "\n");
  94. stdoutMessage.append("copy files time[oprationMillisTime]:" + oprationMillisTime + " MillisTime...\n");
  95. stdoutMessage.append("copy files time[oprationSecondTime]:" + oprationSecondTime + " sencends...\n");
  96. stdoutMessage.append("check more info in this file:" + _backupFolder + "/" + today + "_bak" + "/backupLog.txt\n");
  97. File logFile=new File(_backupFolder + "/backupLog.txt");
  98. logFile.createNewFile();
  99. FileWriter fw = new FileWriter(logFile);
  100. BufferedWriter bw = new BufferedWriter(fw);
  101. bw.write(globalTxtMessage.toString());
  102. bw.flush();
  103. bw.close();
  104. }
  105. catch (Exception e)
  106. {
  107. globalTxtMessage.append("============The main function Exception...==============\n");
  108. e.printStackTrace();
  109. }
  110. return stdoutMessage.toString();
  111. }
  112.  
  113. public static void copyFilesToDirectory(File _updateFolder, File _webappFolder, File _backupFolder) {
  114. String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  115. try {
  116. if (!(_updateFolder.isDirectory())) {
  117. File srcFile = new File(_webappFolder.getAbsolutePath() + _updateFolder.getPath().split(today)[1]);
  118. System.out.println(_webappFolder.getAbsolutePath() + _updateFolder.getPath().split(today)[1]);
  119. totalFileNumber += 1;
  120. if (!(srcFile.exists())) {
  121. totalNewFileNumber += 1;
  122. globalTxtMessage.append("文件:" + srcFile.getPath() + " 在原webapp文件文件夹中不存在,请确认这是个新添加的文件 \n"); return;
  123. }
  124. File destFile = new File(_backupFolder.getAbsolutePath() + _updateFolder.getPath().split(today)[1]);
  125. copyFile(srcFile.getAbsolutePath(), destFile.getAbsolutePath());
  126. totalSuccessCopyNumber += 1; return;
  127. }
  128.  
  129. File[] files = _updateFolder.listFiles();
  130. for (int i = 0; i < files.length; ++i)
  131. {
  132. copyFilesToDirectory(files[i], _webappFolder, _backupFolder);
  133. }
  134. }
  135. catch (Exception e) {
  136. globalTxtMessage.append("复制文件出错! \n");
  137. e.printStackTrace();
  138. }
  139. }
  140.  
  141. public static void copyFile(String oldPath, String newPath)
  142. {
  143. int bytesum = 0;
  144. int byteread = 0;
  145. File oldfile = new File(oldPath);
  146. File newfile = new File(newPath);
  147.  
  148. if (!(newfile.getParentFile().exists())) {
  149. newfile.getParentFile().mkdirs();
  150. }
  151.  
  152. if (oldfile.exists()) {
  153. InputStream inStream = null;
  154. FileOutputStream fs = null;
  155. try {
  156. inStream = new FileInputStream(oldPath);
  157. fs = new FileOutputStream(newPath);
  158.  
  159. byte[] buffer = new byte[1444];
  160. while ((byteread = inStream.read(buffer)) != -1) {
  161. bytesum += byteread;
  162. fs.write(buffer, 0, byteread);
  163. }
  164. }catch (FileNotFoundException e) {
  165. globalTxtMessage.append("复制文件:" + oldfile.getName() + " 时找不到文件! \n");
  166. e.printStackTrace();
  167. }catch (IOException e){
  168. globalTxtMessage.append("复制文件:" + oldfile.getName() + " 时出错! \n");
  169. e.printStackTrace();
  170. } finally {
  171. try {
  172. if (inStream != null)
  173. inStream.close();
  174.  
  175. if (fs != null)
  176. fs.close();
  177. } catch (IOException e) {
  178. e.printStackTrace();
  179. }
  180. }
  181. globalTxtMessage.append("文件复制成功:" + oldfile.getAbsolutePath() + "(size:" + (bytesum / 1024.0D) + "KB)\n");
  182.  
  183. Long oldFileLastModifiedTime = Long.valueOf(oldfile.lastModified());
  184. newfile.setLastModified(oldFileLastModifiedTime.longValue());
  185. } else {
  186. globalTxtMessage.append("文件:" + oldfile.getAbsolutePath() + " 找不到!\n");
  187. }
  188. }
  189. }

运行javac MyBak.java,java MyBak。

这时程序会从旧有项目拷贝一份和更新包相同的文件到bakeupFolder目录。

最后,我们就可以大胆的把更新包在项目中覆盖老文件了。

增量更新项目时的备份MyBak的更多相关文章

  1. svn更新项目时遇到被锁住的问题

    来自:http://blog.csdn.net/woshixuye/article/details/7776742 遇到问题 我们用svn更新某个项目的时候,有时候会遇到一些什么文件夹被locked等 ...

  2. WEBAPI 最近更新项目时 服务器总是提示:An error has occurred.

    解决办法: 在webconfig中设置 <system.web><customErrors mode="Off"/></system.web> ...

  3. 做web项目时对代码改动后浏览器端不生效的应对方法(持续更新)

    做web项目时,常常会遇到改动了代码,但浏览器端没有生效,原因是多种多样的,我会依据我遇到的情况逐步更新解决的方法 1.执行的时候採用debug模式,普通情况下使用项目部署button右边那个butt ...

  4. 做web项目时对代码修改后浏览器端不生效的应对方法(持续更新)

    做web项目时,经常会遇到修改了代码,但浏览器端没有生效,原因是多种多样的,我会根据我遇到的情况逐步更新解决办法 1.运行的时候采用debug模式,一般情况下使用项目部署按钮右边那个按钮下的tomca ...

  5. 解决svn更新项目目录时“Error:svn: E155037: Previous operation has not finished; run 'cleanup' if it was interrupted”的报错问题

    今天在IDEA更新项目目录时,发现报错“Error:svn: E155037: Previous operation has not finished; run 'cleanup' if it was ...

  6. 遇到问题----java----myeclipse或者eclipse发布的项目时配置文件不更新或者无配置文件

    myeclipse或者eclipse发布的项目时配置文件不更新或者无配置文件. 正常的web项目有目录 src/main/resources 和 src/main/java 这两个目录默认在编译发布时 ...

  7. svn更新项目之后,项目报错一大堆并且tomcat部署项目时找不到项目

    原因是:svn更新项目以后jdk路劲不对,需要使用自己安装的jdk,即可.具体步骤如下 第一步:右击项目-->Build path-->Configure Build path... 第二 ...

  8. 前端遇上Go: 静态资源增量更新的新实践

    前端遇上Go: 静态资源增量更新的新实践https://mp.weixin.qq.com/s/hCqQW1F8FngPPGZAisAWUg 前端遇上Go: 静态资源增量更新的新实践 原创: 洋河 美团 ...

  9. 谈谈混合 App Web 资源的打包与增量更新

    综述 移动 App 的运行环境具有带宽不稳定,流量收费,启动速度比较重要等特点,所以混合 App 如何加载 Web 资源并不是一个新问题.本文目的是总结出一种资源打包下载的思路和方案,并且提供一种打包 ...

随机推荐

  1. C++-const_cast只能用于指针和引用,对象的const到非const可以用static_cast

    Static_cast可以对对象也可以对指针也可以对引用,但是const_cast只可以对指针和引用使用,后者不可以对对象用,如果你要把一个const值转化为非const值只能用隐式执行或通过使用st ...

  2. C# 轉義字符

    转义字符 意义 ASCII码值(十进制) \a 响铃(BEL) 007 \b 退格(BS) ,将当前位置移到前一列 008 \f 换页(FF),将当前位置移到下页开头 012 \n 换行(LF) ,将 ...

  3. HDU 1465

    排列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 大家常常 ...

  4. C#基础之类、组件和命名空间(二)

    一.实例化对象 Student s; 首先是在栈中开辟一块空间叫s,s里面的内容是空: s = new Student(); 在堆实例化Student对象,将对象的引用地址保存到栈s里.因此,s指向S ...

  5. C++ primer的第二章的主要内容

    这第二章主要是介绍了C++中基本的内置数据类型:整型与浮点型.介绍了什么是变量的过程中了解到了左值与右值的概念.左值是可以出现在赋值语句的左边或者右边,也就是说可以放在等号的左右两边,而右值只能是出现 ...

  6. 去掉NavigationBar底部的黑线

    UINavigationBar *navigationBar = self.navigationController.navigationBar;   [navigationBar setBackgr ...

  7. hdu 2083

    ps:N个数中,中位数是最小距离...第一次WA是因为排序之后最小和最大相加除2...应该是找他们的中位数,而不是中间数. 代码: #include "stdio.h" #incl ...

  8. openstack 中 log模块分析

    1 . 所在模块,一般在openstack/common/log.py,其实最主要的还是调用了python中的logging模块: 入口函数在 def setup(product_name, vers ...

  9. STL源码分析《4》----Traits技术

    在 STL 源码中,到处可见 Traits 的身影,其实 Traits 不是一种语法,更确切地说是一种技术. STL库中,有一个函数叫做 advance, 用来将某个迭代器(具有指针行为的一种 cla ...

  10. STL 源码分析《2》----nth_element() 使用与源码分析

    Select 问题: 在一个无序的数组中 找到第 n 大的元素. 思路 1: 排序,O(NlgN) 思路 2: 利用快排的 RandomizedPartition(), 平均复杂度是 O(N) 思路 ...