publicvoiddbf(HttpServletRequestrequest,HttpServletResponseresponse){try{response.setCharacterEncoding("utf-8");response.setHeader("content-disposition","attachment;filen...
public void dbf (HttpServletRequest request,HttpServletResponse response){
try {
response.setCharacterEncoding("utf-8");
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("mydata.sql", "UTF-8"));
Runtime rt = Runtime.getRuntime();
String sPath = this.getClass().getResource("/db.properties").getPath() ; //文件路径
String xPath = this.getClass().getResource("/mysqldump.exe").getPath() ; //文件路径
String xxPath = xPath.substring(1);
String dbname = GetProperties.getValue(sPath, "dbname");
String dburl = GetProperties.getValue(sPath, "dburl");
String username = GetProperties.getValue(sPath, "username");
String password = GetProperties.getValue(sPath, "password");
String port = GetProperties.getValue(sPath, "port");
String cmd = "cmd.exe /c "+xxPath+" -h"+dburl+" -P"+port+" -u"+username+" -p"+password+" "+dbname;
Process process=rt.exec(cmd); //C:\\mysqldump\\mysqldump
String line;
InputStreamReader isr = new InputStreamReader(process.getErrorStream());
LineNumberReader input = new LineNumberReader(isr);
while((line = input.readLine())!= null){
System.out.println(line+"~~~~~~~~~~");
}
InputStream in = process.getInputStream();
System.out.println(in.toString());
InputStreamReader xx = new InputStreamReader(in, "utf8");
OutputStream out = null;
int len = 0;
byte[] buffer = new byte[1024];
out = response.getOutputStream();
while((len = in.read(buffer)) > 0) {
out.write(buffer,0,len);
}
out.flush();
out.close();
in.close();
System.out.println(cmd);
System.out.println(sPath);
System.out.println(xPath);
System.out.println(dbname);
System.out.println(dburl);
System.out.println(username);
System.out.println(password);
System.out.println(port);
} catch (IOException e) {
System.out.println("备份失败!");
e.printStackTrace();
}
}
展开