不多说直接上代码,文末有整个demo的源码下载地址
socket服务端代码
public void run() {
BufferedWriter writer = null;
InputStream inputStream = null;
try {
// 设置连接超时5秒
socket.setSoTimeout(5000);
log.info("IDM连接成功");
inputStream = socket.getInputStream();
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
boolean result = false;
try {
result = handle(inputStream);
writer.write(String.valueOf(result));
writer.newLine();
writer.flush();
} catch (IOException | IllegalArgumentException e) {
writer.write("error");
writer.newLine();
writer.flush();
try {
result = handle(inputStream);
writer.write(String.valueOf(result));
writer.newLine();
writer.flush();
} catch (SocketTimeoutException ex) {
log.error("连接超时", ex);
}
}
} catch (SocketException socketException) {
log.error("连接超时", socketException);
try {
inputStream.close();
writer.close();
} catch (IOException ioException) {
log.error("", ioException);
}
} catch (IOException e) {
log.error("", e);
} finally {
try {
inputStream.close();
writer.close();
} catch (IOException e) {
log.error("", e);
}
}
}
调用powershell
PowerShellResponse response = null;
Command command = getCommand(json);
PowerShell session = init(command.getUsername(), command.getPassword(),command.getIp());
if (session != null) {
if (json.contains("Get-Mailbox") || json.contains("Enable-Mailbox")) {
log.info("执行指令:" + command.getCommand());
response = session.executeCommand(command.getCommand());
}
}
exchange/powershell,Java调用powershell开通邮箱-Java文档类资源-CSDN下载