diff --git a/pom.xml b/pom.xml index a5e769b..950ea03 100644 --- a/pom.xml +++ b/pom.xml @@ -96,6 +96,24 @@ mysql-connector-java 8.0.28 + + + smartupload + smartupload + 1.0 + system + ${project.basedir}/src/main/webapp/WEB-INF/lib/jspsmartupload.jar + + + commons-fileupload + commons-fileupload + 1.2.2 + + + commons-io + commons-io + 2.4 + diff --git a/src/main/java/cn/edu/cqwu/repair/controller/admin/AdminDBController.java b/src/main/java/cn/edu/cqwu/repair/controller/admin/AdminDBController.java new file mode 100644 index 0000000..e6970c4 --- /dev/null +++ b/src/main/java/cn/edu/cqwu/repair/controller/admin/AdminDBController.java @@ -0,0 +1,110 @@ +package cn.edu.cqwu.repair.controller.admin; + +import cn.edu.cqwu.repair.db.ConnectionFactory; +import cn.edu.cqwu.repair.util.Message; +import com.jspsmart.upload.SmartUpload; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.context.ServletConfigAware; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * @author xtaod + */ +@Controller +public class AdminDBController implements ServletConfigAware { + + @RequestMapping("/admin/db/backup.do") + private void backUp(HttpServletRequest request, HttpServletResponse response) throws IOException { + ServletContext context = request.getServletContext(); + String path = context.getRealPath("/admin/backup/"); + File file = new File(path); + if (!file.exists()) { + file.mkdirs(); + } + HttpSession session = request.getSession(); + Date date = new Date(); + DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); + String time = format.format(date); + Runtime runtime = Runtime.getRuntime(); + String command = "cmd /c mysqldump -u" + ConnectionFactory.USER + " -p" + ConnectionFactory.PASSWORD + " -h" + ConnectionFactory.HOST + " --column-statistics=0 " + ConnectionFactory.DATABASE + " > " + file.getAbsolutePath() + "\\" + time + ".sql"; + Process process = runtime.exec(command); + try { + int tag = process.waitFor(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream())); + String line; + while ((line = reader.readLine()) != null) { + System.out.println(line); + } + + if (tag == 0) { + session.setAttribute("mess", new Message("backupMess", "备份成功")); + } else { + session.setAttribute("mess", new Message("backupMess", "备份失败")); + } + SmartUpload su = new SmartUpload(); + su.initialize(servletConfig, request, response); + su.setContentDisposition(null); + su.downloadFile("/admin/backup/" + time + ".sql"); + response.getOutputStream().close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @RequestMapping("/admin/db/restore.do") + public String fileUpload(MultipartFile myfile, HttpServletRequest req, Model model) throws IllegalStateException, IOException { + String path = req.getServletContext().getRealPath("/admin/backup/"); + File file = new File(path); + if (!file.exists()) { + file.mkdirs(); + } + Date date = new Date(); + DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); + String time = format.format(date); + String filePath = path + "\\" + time + ".sql"; + myfile.transferTo(new File(filePath)); + String command = "cmd /c mysql -u" + ConnectionFactory.USER + " -p" + ConnectionFactory.PASSWORD + " -h" + ConnectionFactory.HOST + " " + ConnectionFactory.DATABASE + " < \"" + filePath + "\""; + Runtime runtime = Runtime.getRuntime(); + Process process = runtime.exec(command); + try { + int tag = process.waitFor(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream())); + String line; + while ((line = reader.readLine()) != null) { + System.out.println(line); + } + if (tag == 0) { + model.addAttribute("mess", new Message("restoreMess", "还原成功")); + } else { + model.addAttribute("mess", new Message("restoreMess", "还原失败")); + } + } catch (Exception e) { + e.printStackTrace(); + } + return "/admin/db"; + } + + private ServletConfig servletConfig; + + @Override + public void setServletConfig(ServletConfig servletConfig) { + this.servletConfig = servletConfig; + } +} diff --git a/src/main/java/cn/edu/cqwu/repair/db/ConnectionFactory.java b/src/main/java/cn/edu/cqwu/repair/db/ConnectionFactory.java index 0e40a0b..e7acfa8 100644 --- a/src/main/java/cn/edu/cqwu/repair/db/ConnectionFactory.java +++ b/src/main/java/cn/edu/cqwu/repair/db/ConnectionFactory.java @@ -12,9 +12,11 @@ import java.util.HashMap; * @author xtaod */ public class ConnectionFactory { - public static final String URL = "jdbc:mysql://10.151.0.67:3306/repair?useSSL=false&serverTimezone=Asia/Shanghai"; public static final String USER = "root"; public static final String PASSWORD = "J3YNtU6v2dvio3gj6M7r"; + public static final String HOST = "10.151.0.67"; + public static final String DATABASE = "repair"; + public static final String URL = "jdbc:mysql://" + HOST + ":3306/" + DATABASE + "?useSSL=false&serverTimezone=Asia/Shanghai"; public static MybatisFlexBootstrap bootstrap; public static HashMap mappers = new HashMap<>(); diff --git a/src/main/java/cn/edu/cqwu/repair/util/Message.java b/src/main/java/cn/edu/cqwu/repair/util/Message.java new file mode 100644 index 0000000..2abf236 --- /dev/null +++ b/src/main/java/cn/edu/cqwu/repair/util/Message.java @@ -0,0 +1,30 @@ +package cn.edu.cqwu.repair.util; + +/** + * @author xtaod + */ +public class Message { + String name; + String content; + + public Message(String stageAddMess, String s) { + this.name = stageAddMess; + this.content = s; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} diff --git a/src/main/resources/springmvc.xml b/src/main/resources/springmvc.xml index c906a51..ef299ca 100644 --- a/src/main/resources/springmvc.xml +++ b/src/main/resources/springmvc.xml @@ -16,6 +16,8 @@ + + diff --git a/src/main/webapp/WEB-INF/lib/jspsmartupload.jar b/src/main/webapp/WEB-INF/lib/jspsmartupload.jar new file mode 100644 index 0000000..dd5c33e Binary files /dev/null and b/src/main/webapp/WEB-INF/lib/jspsmartupload.jar differ diff --git a/src/main/webapp/admin/db.jsp b/src/main/webapp/admin/db.jsp new file mode 100644 index 0000000..06755ee --- /dev/null +++ b/src/main/webapp/admin/db.jsp @@ -0,0 +1,62 @@ +<%-- + Created by IntelliJ IDEA. + User: xtaod + Date: 2024/5/15 + Time: 下午3:40 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ page isELIgnored="false" %> + + + + + + ${title} + + + + + + + + <%@ include file="../includes/header.jsp" %> + + + + + 备份数据库 + + + ${requestScope.mess.content} + + + + + + + + + 恢复数据库 + + + ${sessionScope.mess.content} + + + + + + 请您上传 sql 文件 + + + + + + + + + + diff --git a/src/main/webapp/includes/menu.jsp b/src/main/webapp/includes/menu.jsp index 9620696..73b5731 100644 --- a/src/main/webapp/includes/menu.jsp +++ b/src/main/webapp/includes/menu.jsp @@ -56,6 +56,11 @@ >> 报修区域管理 + + + >> 设备管理 + + >> 耗材管理 @@ -66,6 +71,11 @@ >> 统计报表 + + + >> 数据库管理 + +