java web和servlet结合使用为数据库备份
使用Runtime类的方法备份数据库和web下载结合起来,就可以得到想要的功能.源代码如下(tomcat+mysql):
package org.liuxuan.jsp;
import java.io.;
import java.text.SimpleDateFormat;
import javax.servlet.;
import javax.servlet.http.*;
public class Backup extends HttpServlet ...{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd"); // 格式化当前系统日期
String dateTime = dateFm.format(new java.util.Date());
File file = null;
InputStream is = null;
String fileName = null;
int fileSize = 0;
String mysql = "mysqldump -uroot -p123456 rudisc > c:\" + dateTime+ ".sql";//mysql备份语句
try ...{
Process p=java.lang.Runtime.getRuntime().exec("cmd /c " + mysql);
while(true)...{
if(p.waitFor()==0) break; //直到进程结束,才退出循环
}
} catch (Exception e) ...{
e.printStackTrace();
}
file = new File("c:\"+dateTime+".sql");
is = new FileInputStream(file);
fileName = file.getName();
fileSize = (int) file.length();
fileName = toUTF8String(fileName);
// 设置下载文件使用的报头域。
response.setContentType("application/x-msdownload");
String str = "attachment; filename=" + fileName;
response.setHeader("Content-Disposition", str);
response.setContentLength(fileSize);
// 得到响应对象的输出流,用于向客户端输出二进制数据。
ServletOutputStream sos = response.getOutputStream();
byte data = new byte;
int len = 0;
while ((len = is.read(data)) > 0) ...{
sos.write(data, 0, len); // 向浏览器输出文件数据
}
is.close(); // 关闭输入流
sos.close();// 关闭输出流
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
doGet(request, response);
}
public static String toUTF8String(String str) ...{
StringBuffer sb = new StringBuffer();
int len = str.length();
for (int i = 0; i < len; i++) ...{
char c = str.charAt(i);
if (c >= 0 && c <= 255) ...{
sb.append(c);
} else ...{
byte b;
try ...{
b = Character.toString(c).getBytes("UTF-8");
} catch (UnsupportedEncodingException ex) ...{
System.err.println(ex);
b = null;
}
for (int j = 0; j < b.length; j++) ...{
int k = b;
if (k < 0) ...{
k &= 255;
}
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}
}
注意1:Runtime类的exec()方法是在单独的进程中执行指定的字符串命令,所以得在该进程结束后,才能找到备份 的sql文件.
注意2:由于是使用响应对象的getOutputStream()方法得到二进制输出流,而假如使用jsp来实现的话,由于隐含对象out的存在,很有可能导致冲突,异常的发生.所以我使用的是servlet来实现该功能.
数据库的恢复: public static void load() { try { String fPath = "d:/xytx.sql"; Runtime rt = Runtime.getRuntime();
// 调用 mysql 的 cmd: Process child = rt.exec("C:/Program Files/MySQL/MySQL Server 5.0/bin/mysql.exe -uroot -proot xytx "); OutputStream out = child.getOutputStream();//控制台的输入信息作为输出流 String inStr; StringBuffer sb = new StringBuffer(""); String outStr; BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(fPath), "utf8")); while ((inStr = br.readLine()) != null) { sb.append(inStr + "\r\n"); } outStr = sb.toString();
OutputStreamWriter writer = new OutputStreamWriter(out, "utf8"); writer.write(outStr); // 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免 writer.flush(); // 别忘记关闭输入输出流 out.close(); br.close(); writer.close();
System.out.println("");
} catch (Exception e) { e.printStackTrace(); }
}