///实体类
package com.ews.util;
/**
* 系统备份展示对象
*
* */
public class DataFile {
private String fileName;//备份文件的名称
private String fileDate;//备份文件的日期
private String filePath;//备份文件的地址
private String fileSize;//备份文件的大小
public String getFileSize() {
return fileSize;
}
public void setFileSize(String fileSize) {
this.fileSize = fileSize;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileDate() {
return fileDate;
}
public void setFileDate(String fileDate) {
this.fileDate = fileDate;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
}
///实现备份代码
package com.ews.action;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Date;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.fileupload.FileItem;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ews.util.DataFile;
public class DataAction extends EwsAction{
private String username;
private String password;
private String host;
private String PORT;
private String dbname;
private List dataFiles = new ArrayList();
private File reductionFile;
public File getReductionFile() {
return reductionFile;
}
public void setReductionFile(File reductionFile) {
this.reductionFile = reductionFile;
}
public List getDataFiles() {
return dataFiles;
}
public void setDataFiles(List dataFiles) {
this.dataFiles = dataFiles;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getPORT() {
return PORT;
}
public void setPORT(String pORT) {
PORT = pORT;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDbname() {
return dbname;
}
public void setDbname(String dbname) {
this.dbname = dbname;
}
/**
* 删除
* */
public String delete(){
String fileName = request.getParameter("fileName");
System.out.println(fileName);
String backPath = ServletActionContext.getServletContext().getRealPath("/")+"ewssite/back/"+fileName;
File file = new File(backPath);
file.delete();
return "delete";
}
/**
* 得到备份文件的List集合
*
* */
public String findList(){
String backPath = ServletActionContext.getServletContext().getRealPath("/")+"ewssite/back/";
File file = new File(backPath);
if (!file.exists())
return "findListData";
File[] file1 = file.listFiles();
for (int i = 0; i < file1.length; i++) {
if(file1[i].getName().equals("ramdit.txt")) continue;
SimpleDateFormat sdf= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
//前面的lSysTime是秒数,先乘1000得到毫秒数,再转为java.util.Date类型
java.util.Date dt = new Date(file1[i].lastModified());
String sDateTime = sdf.format(dt); //得到精确到秒的表示:08/31/2006 21:08:00
DataFile dataFile = new DataFile();
dataFile.setFileName(file1[i].getName());
dataFile.setFileDate(sDateTime);
String path = request.getContextPath();
String filePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/ewssite/back/"+file1[i].getName();
dataFile.setFilePath(filePath);
DecimalFormat df = new DecimalFormat( ".## ");
dataFile.setFileSize(df.format(file1[i].length()/1024000f));
dataFiles.add(dataFile);
}
return "findListData";
}
/**
* 配置 Mysql bin目录
* */
public void getConfig(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BasicDataSource ba = (BasicDataSource)context.getBean("dataSource");
setUsername(ba.getUsername());
setPassword(ba.getPassword());
String url = ba.getUrl();
url = url.substring(13, url.length());
String[] temp = url.split("/");
String[] temp1 = temp[0].split(":");
setHost(temp1[0]);
setPORT(temp1[1]);
for (int i = 0; i < temp[1].length(); i++) {
String temp2 = temp[1].charAt(i)+"";
if(temp2.equals("?")){
setDbname(temp[1].substring(0,5));
}
}
}
/**
* 备份
* */
public String backup(){
getConfig();
//得到配置文件
try {
Runtime rt = Runtime.getRuntime();
String backPath = ServletActionContext.getServletContext().getRealPath("/")+"ewssite/back/"+System.currentTimeMillis()+".sql";
String mysql = "mysqldump -u" + getUsername()+ " -p" + getPassword() + " --default-character-set=utf8 -h"+getHost()+" -P"+getPORT()+" " + getDbname() +" >"+"\""+backPath+"\"";
Process proc = rt.exec("cmd.exe /c "+mysql);// 设置导出编码为utf8。这里必须是utf8
//String backExe = ServletActionContext.getServletContext().getRealPath("/")+"bin/mysqldump.exe";
//String mysql = getDbname()+ " -u" + getUsername()+ " -p" + getPassword() + " --default-character-set=utf8 -h"+getHost()+" -P"+getPORT()+" >"+"\""+backPath+"\"";
int tag = proc.waitFor();// 等待进程终止
} catch (Exception e) {
e.printStackTrace();
}
return "backup";
}
/**
* 还原
* */
public String load(){
String sqlPath="";
if(request.getParameter("selectName")!=null)
sqlPath = request.getParameter("selectName");
if(reductionFile!=null){
String name = upload(reductionFile);
sqlPath = ServletActionContext.getServletContext().getRealPath("/")+"ewssite/back/" + name;
}
// System.out.println(sqlPath);
if(sqlPath.substring(sqlPath.lastIndexOf(".")+1).equals("sql")){
getConfig();
setHost("127.0.0.1");
setUsername("root");
setPassword("root");
setDbname("test");
//得到配置文件
try {
Runtime rt = Runtime.getRuntime();
String createDb = "mysqladmin -u" + getUsername()+ " -p" + getPassword() + " create "+getDbname();
String mysql = "mysql -u" + getUsername()+ " -p" + getPassword() + " "+getDbname()+" <"+"\""+ sqlPath+"\"";//+"\""+backPath+"\""
rt.exec("cmd.exe /c "+createDb);
Process proc = rt.exec("cmd.exe /c "+mysql);
int tag = proc.waitFor();// 等待进程终止
} catch (Exception e) {
e.printStackTrace();
}
}
return "load";
}
/**
* 把本地的数据库备份文件上传到服务器上
* file:从前台获取的file
* */
public String upload(File file ){
String name = "";
try {
DataInputStream in = new DataInputStream(new FileInputStream(file));
// FileInputStream in = new FileInputStream(file);
String backPath = ServletActionContext.getServletContext().getRealPath("/")+"ewssite/back/";
name = System.currentTimeMillis()+".sql";
backPath = backPath + name;
// FileOutputStream out = new FileOutputStream(new File(backPath));
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(backPath)));
int b = -1;
while ((b = in.read()) != -1) {
out.write(b);
}
out.close();
in.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return name;
}
} 相关文章
PhpStorm如何使用代码片段变量
VSCode自定义悬停模板怎么套用?高效开发配置方案
GoLand怎么把代码片段Live Templates保存下来
Sublime Text怎么跳转到自定义代码片段所在的文件
Windows平台Navicat已保存的代码段如何在Linux环境中复用?
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。
60
2026.11.12
本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。
40
2026.11.12
本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。
20
2026.11.12
本专题从实际API接口调试场景出发,讲解Postman身份认证的完整配置流程,包括常见认证协议介绍、授权参数设置、访问令牌填写、OAuth 2.0授权及认证失败排查,帮助用户正确完成接口授权与访问验证。
140
2026.11.06
围绕Postman HTTP请求头配置展开,讲解Headers选项的具体使用方式,包括Key和Value参数填写、请求头信息添加、常见Header字段设置及请求头修改方法,帮助用户解决接口调试过程中遇到的请求头配置问题。
120
2026.11.06
详细介绍Postman接口请求的创建与发送方法,涵盖GET和POST请求、URL配置、请求参数、Headers请求头、Body请求体设置以及响应结果查看,帮助用户掌握Postman接口请求的完整操作流程。
100
2026.11.06
DBeaver数据导入专题,整理Excel、CSV、SQL、TXT等常见文件的数据导入方法,涵盖目标数据库选择、数据表创建、字段映射、数据类型设置及导入参数配置等操作,帮助用户将本地文件中的数据写入数据库,并了解批量导入、重复数据处理和导入失败等常见问题的解决方法。
100
2026.11.05
DBeaver安全防护专题,围绕数据库连接安全、账号密码保护和访问权限管理展开,整理SSL/TLS加密连接、SSH隧道、数据库凭据保存及证书验证等配置方法,同时介绍敏感信息保护、远程连接风险和高权限账号使用等注意事项,帮助用户提高数据库日常管理的安全性。
80
2026.11.05
本专题聚焦DBeaver数据库连接配置与管理,覆盖从新建连接、选择数据库类型到填写连接参数和验证连接的完整流程。针对MySQL、PostgreSQL、SQL Server等常见数据库,整理相应的连接设置方法,并提供驱动缺失、认证失败和连接异常等问题的排查方向。
80
2026.11.04
热门下载
相关下载
精品课程
共1课时 | 219人学习
共0课时 | 0人学习
共1课时 | 186人学习
最新文章








