后端生成rdp文件并用cmd打开
调用:
public String openRdp(String ip){
String res = this.createRdpFile(ip);
if ("error".equals(res) || StringUtils.isBlank(res)) {
return "生成rdp文件失败!";
}
// 运行rdp
if (!this.startCmd(res)) {
return "生成的rdp无法启动,请联系管理员!";
}
}
生成rdp文件:
/**
* 创建本地rdp文件
* 基础版本
* @author ChengXY
* @date 2023年2月24日下午4:38:29
*/
private String createRdpFile(String serverIp) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String dateStr = sdf.format(new Date());
String path = "D:/tmp/"+dateStr + ".rdp";
// rdp里的内容可以百度参数具体含义,视自己情况编写
String fileContent = "auto connect:i:1\r\n"
+ "full address:s:"+serverIp+"\r\n"
+ "username:s:Administrator\r\n"
+ "remoteapplicationmode:i:1\r\n"
+ "remoteapplicationname:s:Abaqus\r\n"
+ "remoteapplicationprogram:s:c:\\SDM_init.bat\r\n"
+ "remoteapplicationcmdline:s:guochangyou liuyu ";
File file = new File(path);
if (!file.exists()) {
try {
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
BufferedWriter bw = new BufferedWriter(osw);
bw.write(fileContent);
bw.flush();
bw.close();
osw.close();
fos.close();
} catch (Exception e) {
return "error";
}
return path;
}
return null;
}
启动cmd:
/**
* 启动cmd
*
* @author ChengXY
* @date 2023年2月27日上午11:38:08
*/
public Boolean startCmd(String file) {
boolean flag = false;
try {
Runtime.getRuntime().exec("cmd.exe /c" + file);
flag = true;
} catch (Exception e) {
logger.error(e.getMessage());
}
return flag;
}