⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 backupdbaction.java

📁 SSH示范
💻 JAVA
字号:
package com.iplan.portal.order.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.iplan.portal.framework.base.struts.BaseAction;
import com.iplan.portal.framework.utils.DateUtil;

/**
 * http://www.hao-se.cn
 * 
 * @author ws
 */
public class BackUpDBAction extends BaseAction {
	private final static String DEFAULT_DB_PATH = "{user.home}/iplan/db";

	public ActionForward save(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) throws Exception {
		ActionMessages messages = new ActionMessages();
		
		try {
			ServletContext sc = httpServletRequest.getSession()
					.getServletContext();
			String dbPath = getDbPath(sc, "hsql.dbPath");
			String backupPath = getDbPath(sc, "backup.db");
			String dbName = sc.getInitParameter("hsql.dbName");

			// 源
			if (!dbPath.endsWith("/")) {
				dbPath = dbPath + "/";
			}
			File dbFileScript = new File(dbPath + dbName + ".script");
			File dbFileProperties = new File(dbPath + dbName + ".properties");
			File dbFileLog = new File(dbPath + dbName + ".log");

			String postfix = DateUtil.format(new Date(), "yyyy-MM-dd.HHmmss");
			postfix = ".".concat(postfix);
			// 需要备份的
			if (!backupPath.endsWith("/")) {
				backupPath = backupPath + "/";
			}
			File backupDbFileScript = new File(backupPath
					+ dbName.concat(postfix) + ".script");
			File backupDbFileProperties = new File(backupPath
					+ dbName.concat(postfix) + ".properties");
			File backupDbFileLog = new File(backupPath + dbName.concat(postfix)
					+ ".log");

			copy(new FileInputStream(dbFileScript), new FileOutputStream(
					backupDbFileScript));
			copy(new FileInputStream(dbFileProperties), new FileOutputStream(
					backupDbFileProperties));
			copy(new FileInputStream(dbFileLog), new FileOutputStream(
					backupDbFileLog));
			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("success.backupDB"));
		} catch (Exception e) {
			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.backupDB"));
			e.printStackTrace();
		}
		saveMessages(httpServletRequest, messages); 
		
		return actionMapping.findForward("success");
	}

	private String getDbPath(ServletContext sc, String param) {
		String path = sc.getInitParameter(param);

		if (StringUtils.isEmpty(path)) {
			path = DEFAULT_DB_PATH;
		}
		if (path.startsWith("{user.home}")) {
			path = path.replaceFirst("\\{user.home\\}", System.getProperty(
					"user.home").replace('\\', '/'));
		}
		if (path.startsWith("{webIPlan.root}")) {
			path = path.replaceFirst("\\{webIPlan.root\\}", sc.getRealPath("/")
					.replace('\\', '/'));
		}
		return path;
	}

	private static boolean copy(InputStream in1, OutputStream out1) {
		try {
			byte[] bytes = new byte[65536];
			int c;
			while ((c = in1.read(bytes)) != -1) {
				out1.write(bytes, 0, c);
			}
			in1.close();
			out1.close();
			return true; 
		}

		catch (Exception e) {
			return false;
		} finally {
			try {
				in1.close();
				out1.close();
			} catch (Throwable t) {

			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -