java window7 以上的操作系统调用cmd需要超级管理员怎么解决在线等。
publicvoiddbf(HttpServletRequestrequest,HttpServletResponseresponse){try{response.set...
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();
}
} 展开
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();
}
} 展开
展开全部
Runas /User:Administrator "cmd.exe"
这时会提示输入 Administrator 的密码。我们从 Process 的 OutputStream 写入密码让它登录账户继续。
另外,你这个 Java 程序是运行在前台么?如果是运行在服务中并使用了不同的账户有”允许与桌面交互“么?我也不清楚哪种情况下不需要 Administrator 权限,不过可以多试几种设置。
这时会提示输入 Administrator 的密码。我们从 Process 的 OutputStream 写入密码让它登录账户继续。
另外,你这个 Java 程序是运行在前台么?如果是运行在服务中并使用了不同的账户有”允许与桌面交互“么?我也不清楚哪种情况下不需要 Administrator 权限,不过可以多试几种设置。
更多追问追答
追问
已经解决了。thanks。
追答
说说怎么解决的,我没用 Windows 7,没体验过这个案例啊。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询