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

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

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date; public class MyBak
{
private static StringBuffer globalTxtMessage = new StringBuffer("");
private static StringBuffer stdoutMessage = new StringBuffer("");
private static int totalFileNumber = 0;
private static int totalSuccessCopyNumber = 0;
private static int totalNewFileNumber = 0; public static void main(String[] args)
{
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); String webappFolder = "/www/htdocs/webapps/M4"; String updateFolder = "/home/whaty/bakeForUpdate/update/" + today; String backupFolder = "/home/whaty/bakeForUpdate/bake/" + today; if (webappFolder.equals(backupFolder)) {
stdoutMessage.append("you can't just copy thins to the webapps folder!\n");
globalTxtMessage = new StringBuffer("");
stdoutMessage = new StringBuffer("");
} else {
String msg = backupFiles(updateFolder, webappFolder, backupFolder);
System.out.print(msg);
globalTxtMessage = new StringBuffer("");
stdoutMessage = new StringBuffer("");
}
} public static String backupFiles(String _updateFolder, String _webappFolder, String _backupFolder)
{
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String now = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
try {
File updateFolder = new File(_updateFolder);
File webappFolder = new File(_webappFolder);
File backupFolder = new File(_backupFolder); globalTxtMessage.append("============" + now + "Backup Log==========\n");
globalTxtMessage.append("============初始化条件测试开始===============\n");
stdoutMessage.append("============init test start===============\n"); if (!(updateFolder.exists())) {
globalTxtMessage.append(_updateFolder + "更新包文件夹找不到!\n");
stdoutMessage.append(" the upload folder \"" + _updateFolder + "\" can Not found! \n please make sure you have upload the folder!!!!\n");
}
if (!(webappFolder.exists())) {
globalTxtMessage.append("项目Webapp文件夹" + _webappFolder + "找不到!\n");
stdoutMessage.append("the Webapp folder " + _webappFolder + " can Not found!!!\n please make sure the folder is exists!!!!\n");
return stdoutMessage.toString();
}
if (!(backupFolder.exists())) {
globalTxtMessage.append("自动创建备份文件夹" + backupFolder.getAbsolutePath() + "\n");
stdoutMessage.append("auto create folder " + backupFolder.getAbsolutePath() + "\n");
backupFolder.mkdirs();
}
globalTxtMessage.append("============初始化条件测试结束===============\n");
stdoutMessage.append("============init test end===============\n"); Long start = Long.valueOf(System.currentTimeMillis()); copyFilesToDirectory(updateFolder, webappFolder, backupFolder); Long end = Long.valueOf(System.currentTimeMillis()); Long oprationMillisTime = Long.valueOf(end.longValue() - start.longValue());
Long oprationSecondTime = Long.valueOf((end.longValue() - start.longValue()) / 1000L); globalTxtMessage.append("备份完成!\n");
globalTxtMessage.append("=========================================\n");
globalTxtMessage.append("总文件数:" + totalFileNumber + "\n");
globalTxtMessage.append("成功复制:" + totalSuccessCopyNumber + "\n");
globalTxtMessage.append("新增文件数:" + totalNewFileNumber + "\n");
globalTxtMessage.append("拷贝文件所需时间[oprationMillisTime]:" + oprationMillisTime + " 毫秒\n");
globalTxtMessage.append("拷贝文件所需时间[oprationSecondTime]:" + oprationSecondTime + " 秒\n"); stdoutMessage.append("backup done!\n");
stdoutMessage.append("total files number:" + totalFileNumber + "\n");
stdoutMessage.append("success copies number:" + totalSuccessCopyNumber + "\n");
stdoutMessage.append("new files number:" + totalNewFileNumber + "\n");
stdoutMessage.append("copy files time[oprationMillisTime]:" + oprationMillisTime + " MillisTime...\n");
stdoutMessage.append("copy files time[oprationSecondTime]:" + oprationSecondTime + " sencends...\n");
stdoutMessage.append("check more info in this file:" + _backupFolder + "/" + today + "_bak" + "/backupLog.txt\n");
File logFile=new File(_backupFolder + "/backupLog.txt");
logFile.createNewFile();
FileWriter fw = new FileWriter(logFile);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(globalTxtMessage.toString());
bw.flush();
bw.close();
}
catch (Exception e)
{
globalTxtMessage.append("============The main function Exception...==============\n");
e.printStackTrace();
}
return stdoutMessage.toString();
} public static void copyFilesToDirectory(File _updateFolder, File _webappFolder, File _backupFolder) {
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
try {
if (!(_updateFolder.isDirectory())) {
File srcFile = new File(_webappFolder.getAbsolutePath() + _updateFolder.getPath().split(today)[1]);
System.out.println(_webappFolder.getAbsolutePath() + _updateFolder.getPath().split(today)[1]);
totalFileNumber += 1;
if (!(srcFile.exists())) {
totalNewFileNumber += 1;
globalTxtMessage.append("文件:" + srcFile.getPath() + " 在原webapp文件文件夹中不存在,请确认这是个新添加的文件 \n"); return;
}
File destFile = new File(_backupFolder.getAbsolutePath() + _updateFolder.getPath().split(today)[1]);
copyFile(srcFile.getAbsolutePath(), destFile.getAbsolutePath());
totalSuccessCopyNumber += 1; return;
} File[] files = _updateFolder.listFiles();
for (int i = 0; i < files.length; ++i)
{
copyFilesToDirectory(files[i], _webappFolder, _backupFolder);
}
}
catch (Exception e) {
globalTxtMessage.append("复制文件出错! \n");
e.printStackTrace();
}
} public static void copyFile(String oldPath, String newPath)
{
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
File newfile = new File(newPath); if (!(newfile.getParentFile().exists())) {
newfile.getParentFile().mkdirs();
} if (oldfile.exists()) {
InputStream inStream = null;
FileOutputStream fs = null;
try {
inStream = new FileInputStream(oldPath);
fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
fs.write(buffer, 0, byteread);
}
}catch (FileNotFoundException e) {
globalTxtMessage.append("复制文件:" + oldfile.getName() + " 时找不到文件! \n");
e.printStackTrace();
}catch (IOException e){
globalTxtMessage.append("复制文件:" + oldfile.getName() + " 时出错! \n");
e.printStackTrace();
} finally {
try {
if (inStream != null)
inStream.close(); if (fs != null)
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
globalTxtMessage.append("文件复制成功:" + oldfile.getAbsolutePath() + "(size:" + (bytesum / 1024.0D) + "KB)\n"); Long oldFileLastModifiedTime = Long.valueOf(oldfile.lastModified());
newfile.setLastModified(oldFileLastModifiedTime.longValue());
} else {
globalTxtMessage.append("文件:" + oldfile.getAbsolutePath() + " 找不到!\n");
}
}
}

运行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. UVA 10970-Big Chocolate

    题目: 给你一块M*N的巧克力,问把它切成最小单元需要最少切几刀,分开的就不能一起切了. 分析: 每次切割只能多产生一个部分,分成M*N个部分,必然要切M*N-1刀. 一个长为m宽为n的长方形和m*n ...

  2. lightoj1085 线段树+dp

    //Accepted 7552 KB 844 ms //dp[i]=sum(dp[j])+1 j<i && a[j]<a[i] //可以用线段树求所用小于a[i]的dp[j ...

  3. OkHttp+Stetho+Chrome调试android网络部分

    如果要达到上面的效果,你需要改造你的网络请求模块,使用Chrome浏览器和android程序之间的中间件来连接,这就是本篇要介绍的主题:OkHttp+Stetho+Chrome进行网络调试. okht ...

  4. SmartZoneOCR识别控件免费下载地址

    SmartZone™光学字符识别工具包,使开发人员能够进行带状区域光学字符识别,带状区域在表单处理应用程序中经常使用.本产品所包含的.NET控件以及ActiveX COM组件在内部使用两种单独的识别技 ...

  5. iOS 从相机或相册获取图片并裁剪

    今天遇到一个用户头像上传的问题,需要从相册或者相机中读取图片.代码很简单,抽取关键部分,如下: //load user image - (void)UesrImageClicked { UIActio ...

  6. GIT之二 基础篇(1)

    GIT基础 取得项目的 Git 仓库 有两种取得 Git 项目仓库的方法.第一种是在现存的目录下,通过导入所有文件来创建新的 Git 仓库.第二种是从已有的 Git 仓库克隆出一个新的镜像仓库来. 在 ...

  7. Quirks模式是什么?

    什么是DOCTYPE: DOCTYPE,或者称为 Document Type Declaration(文档类型声明,缩写 DTD).通常情况下,DOCTYPE 位于一个 HTML 文档的最前面的 位置 ...

  8. Ecshop安装过程中的的问题:cls_image::gd_version()和不支持JPEG

    在安装Ecshop的时候,遇到两个问题: 1.Strict Standards: Non-static method cls_image::gd_version() should not be cal ...

  9. 对dataTable去重

    using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.T ...

  10. AIO、NIO、BIO

    AIO:异步非阻塞 NIO:同步非阻塞 BIO:同步阻塞 (1)同步 指的是用户进程触发IO操作并等待或者轮询的去查看IO操作是否就绪 (2)异步 指用户进程触发IO操作以后便开始做自己的事情,而当I ...