Java cmd打开rdp远程连接

后端生成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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值