Files
klp-oa/scaner/src/main/java/org/example/SCTool.java

103 lines
3.5 KiB
Java
Raw Normal View History

2025-08-23 17:22:06 +08:00
package org.example;
import org.json.JSONException;
import org.json.JSONObject;
import java.awt.*;
public class SCTool {
/**
* 判断字符串是否为JSON格式
*/
public static boolean isJson(String str) {
if (str == null || str.isEmpty()) return false;
str = str.trim();
return (str.startsWith("{") && str.endsWith("}")) || (str.startsWith("[") && str.endsWith("]"));
}
/**
* 校验字符串是否为有效JSON
*/
public static boolean isValidJson(String input) {
try {
new JSONObject(input);
return true;
} catch (JSONException e) {
System.out.println("JSON解析失败原因: " + e.getMessage());
return false;
}
}
/**
* 锁定英文输入法Windows系统专用
*/
public static void lockEnglishInputMethod() {
try {
String powerShellCmd = "powershell -Command \"$im = Get-WinUserLanguageList | Where-Object LanguageTag -eq 'en-US'; " +
"if ($im) { Set-WinUserLanguageList -LanguageList $im -Force; Write-Host 'Success' }\"";
Process process = Runtime.getRuntime().exec(powerShellCmd);
process.waitFor();
if (process.exitValue() == 0) {
System.out.println("英文输入法已锁定en-US");
} else {
System.out.println("PowerShell切换输入法失败尝试备选方案Ctrl+空格)");
sendKeyStroke(0x11, 0x20);
}
} catch (Exception e) {
System.out.println("设置英文输入法异常: " + e.getMessage());
e.printStackTrace();
}
}
/**
* 发送键盘组合键用于输入法切换备选方案
*/
public static void sendKeyStroke(int modifier, int key) {
try {
Robot robot = new Robot();
robot.keyPress(modifier);
robot.keyPress(key);
robot.keyRelease(key);
robot.keyRelease(modifier);
System.out.println("已尝试Ctrl+空格切换输入法");
} catch (AWTException e) {
System.out.println("发送键盘事件失败: " + e.getMessage());
}
}
/**
* 处理JSON数据并发送POST请求
*/
public static void sendDataToServer(String jsonInput, String userIdentity) {
try {
JSONObject json = new JSONObject(jsonInput);
json.put("createBy", userIdentity);
String finalJson = json.toString(2);
System.out.println("最终发送的JSON数据: ");
System.out.println(finalJson + "\n");
// 取消注释以下代码以启用HTTP请求
String response = HttpRequestUtil.postJson(
"http://140.143.206.120:8080/wms/stockIoDetail",
finalJson
);
if (response == null || response.isEmpty()) {
System.out.println("服务器无响应,请检查接口连接\n");
} else {
System.out.println("服务器返回结果: ");
String formattedResponse = SCTool.isJson(response) ? new JSONObject(response).toString(2) : response;
System.out.println(formattedResponse + "\n");
}
} catch (JSONException e) {
System.out.println("JSON处理异常: " + e.getMessage() + "\n");
} catch (Exception e) {
System.out.println("发送请求异常: " + e.getMessage() + "\n");
}
}
}