feat: 乱起八糟的东西

This commit is contained in:
砂糖
2025-08-23 17:22:06 +08:00
parent 5ed6436713
commit c1f3c2d7d6
8 changed files with 254 additions and 222 deletions

View File

@@ -12,7 +12,6 @@ import org.json.JSONException;
import org.json.JSONObject;
public class Main {
static HttpRequestUtil http = null;
private static String userIdentity;
// 存储扫码枪输入的字符
private static final BlockingQueue<Character> inputQueue = new ArrayBlockingQueue<>(4096);
@@ -24,7 +23,7 @@ public class Main {
public static void main(String[] args) throws InterruptedException {
System.out.print(" ***************程序开始****************** \r\n");
lockEnglishInputMethod();
SCTool.lockEnglishInputMethod();
// 使用BufferedReader获取用户身份避免关闭System.in
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
@@ -45,9 +44,6 @@ public class Main {
startRawInputListener();
startScanDataProcessor();
// 初始化HTTP请求工具
http = new HttpRequestUtil();
System.out.println("HTTP请求工具创建完成");
System.out.println("提示1. 使用扫码枪扫描JSON数据2. 输入 'exit' 并回车退出程序\n");
// 主线程阻塞直到isExit被标记为true
@@ -121,9 +117,9 @@ public class Main {
}
// 校验JSON格式
if (isValidJson(scanInput)) {
if (SCTool.isValidJson(scanInput)) {
System.out.println("JSON格式校验通过开始处理...");
sendDataToServer(scanInput);
SCTool.sendDataToServer(scanInput, userIdentity);
} else {
System.out.println("错误输入不是有效JSON格式\n");
}
@@ -144,98 +140,4 @@ public class Main {
processorThread.setDaemon(true);
processorThread.start();
}
/**
* 校验字符串是否为有效JSON
*/
private static boolean isValidJson(String input) {
try {
new JSONObject(input);
return true;
} catch (JSONException e) {
System.out.println("JSON解析失败原因: " + e.getMessage());
return false;
}
}
/**
* 锁定英文输入法Windows系统专用
*/
private 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();
}
}
/**
* 发送键盘组合键(用于输入法切换备选方案)
*/
private 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请求
*/
private static void sendDataToServer(String jsonInput) {
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 = 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");
}
}
/**
* 判断字符串是否为JSON格式
*/
private static boolean isJson(String str) {
if (str == null || str.isEmpty()) return false;
str = str.trim();
return (str.startsWith("{") && str.endsWith("}")) || (str.startsWith("[") && str.endsWith("]"));
}
}

View File

@@ -0,0 +1,102 @@
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");
}
}
}