//数据备份
@ResponseBody
@GetMapping("/backups")
@RequiresPermissions("system:data:backups")
public void list(HttpServletResponse response) {
//备份文件
String filePath="C:\\beifen";//备份文件的路径
String dbName="log_system233"+(new Date().getTime());//备份文件的名称
try {
Process process = Runtime.getRuntime().exec(
"cmd /c mysqldump -u root -proot " + "log_system233" + " > "
+ filePath + "/" + dbName
+ ".sql");
//备份的数据库名字为log_system,数据库连接和密码均为root
} catch (Exception e) {
e.printStackTrace();
}
//下载备份的文件
String filename = "C:\\beifen/";
String filenameZip = filename + dbName + ".sql";//获取文件的路径
try {
response.setContentType("application/x-execl");
response.setHeader("Content-Disposition",
"attachment;filename=" + new String((dbName+".sql").getBytes(), "UTF-8"));
// 读取文件
InputStream in = new FileInputStream(filenameZip);
ServletOutputStream outputStream = response.getOutputStream();
// 写文件
int b;
while ((b = in.read()) != -1) {
outputStream.write(b);
}
in.close();
outputStream.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//结束
}
如果在程序程加上断点,那么页面下载的就不是空文件了;但不加断点的话,页面下载的就是空文件