2025-07-31 14:44:10 +08:00
|
|
|
|
package org.example;
|
|
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
import java.awt.AWTException;
|
|
|
|
|
|
import java.awt.Robot;
|
|
|
|
|
|
import java.io.BufferedReader;
|
2025-07-31 14:44:10 +08:00
|
|
|
|
import java.io.IOException;
|
2025-08-23 13:56:54 +08:00
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
|
|
|
|
|
import java.util.concurrent.BlockingQueue;
|
2025-07-31 14:44:10 +08:00
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
|
import org.json.JSONObject;
|
2025-07-31 14:44:10 +08:00
|
|
|
|
|
|
|
|
|
|
public class Main {
|
2025-08-22 13:19:22 +08:00
|
|
|
|
static HttpRequestUtil http = null;
|
2025-08-23 13:56:54 +08:00
|
|
|
|
private static String userIdentity;
|
|
|
|
|
|
// 存储扫码枪输入的字符
|
|
|
|
|
|
private static final BlockingQueue<Character> inputQueue = new ArrayBlockingQueue<>(4096);
|
|
|
|
|
|
// 扫码枪结束符(回车)的ASCII值
|
|
|
|
|
|
private static final int ENTER_ASCII = 13; // \r的ASCII值
|
|
|
|
|
|
private static final int NEWLINE_ASCII = 10; // \n的ASCII值
|
|
|
|
|
|
private static volatile boolean isExit = false;
|
2025-07-31 14:44:10 +08:00
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
public static void main(String[] args) throws InterruptedException {
|
|
|
|
|
|
System.out.print(" ***************程序开始****************** \r\n");
|
2025-08-15 11:42:31 +08:00
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
lockEnglishInputMethod();
|
2025-08-15 11:42:31 +08:00
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
// 使用BufferedReader获取用户身份,避免关闭System.in
|
|
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
|
|
|
|
|
|
try {
|
|
|
|
|
|
System.out.print("请输入您的身份标识(如:admin): ");
|
|
|
|
|
|
userIdentity = reader.readLine().trim();
|
|
|
|
|
|
while (userIdentity.isEmpty()) {
|
|
|
|
|
|
System.out.print("身份标识不能为空,请重新输入: ");
|
|
|
|
|
|
userIdentity = reader.readLine().trim();
|
|
|
|
|
|
}
|
|
|
|
|
|
System.out.println("已保存身份信息: " + userIdentity + "\n");
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
System.out.println("读取用户身份时出错: " + e.getMessage());
|
|
|
|
|
|
System.exit(1);
|
2025-07-31 14:44:10 +08:00
|
|
|
|
}
|
2025-08-15 11:42:31 +08:00
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
// 启动输入监听和数据处理线程
|
|
|
|
|
|
startRawInputListener();
|
|
|
|
|
|
startScanDataProcessor();
|
2025-07-31 14:44:10 +08:00
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
// 初始化HTTP请求工具
|
|
|
|
|
|
http = new HttpRequestUtil();
|
|
|
|
|
|
System.out.println("HTTP请求工具创建完成");
|
|
|
|
|
|
System.out.println("提示:1. 使用扫码枪扫描JSON数据;2. 输入 'exit' 并回车退出程序\n");
|
|
|
|
|
|
|
|
|
|
|
|
// 主线程阻塞(直到isExit被标记为true)
|
|
|
|
|
|
while (!isExit) {
|
|
|
|
|
|
Thread.sleep(100);
|
2025-08-15 11:42:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
// 程序退出清理
|
|
|
|
|
|
System.out.println("\n开始停止所有服务...");
|
|
|
|
|
|
isExit = true;
|
|
|
|
|
|
System.out.println("程序已正常退出");
|
2025-08-15 11:42:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 输入监听线程:记录所有接收的字符
|
|
|
|
|
|
*/
|
|
|
|
|
|
private static void startRawInputListener() {
|
|
|
|
|
|
Thread inputThread = new Thread(() -> {
|
2025-07-31 14:44:10 +08:00
|
|
|
|
try {
|
2025-08-23 13:56:54 +08:00
|
|
|
|
int byteData;
|
|
|
|
|
|
while (!isExit && (byteData = System.in.read()) != -1) {
|
|
|
|
|
|
char c = (char) byteData;
|
|
|
|
|
|
// 打印每个接收的字符(含ASCII值),用于调试
|
|
|
|
|
|
System.out.printf("[接收字符] 内容: '%c' ASCII: %d%n",
|
|
|
|
|
|
(c == '\r' ? '\\' + 'r' : c == '\n' ? '\\' + 'n' : c),
|
|
|
|
|
|
(int) c);
|
|
|
|
|
|
|
|
|
|
|
|
inputQueue.put(c);
|
2025-08-15 11:42:31 +08:00
|
|
|
|
}
|
2025-07-31 14:44:10 +08:00
|
|
|
|
} catch (IOException e) {
|
2025-08-23 13:56:54 +08:00
|
|
|
|
if (!isExit) {
|
|
|
|
|
|
System.out.println("输入监听异常: " + e.getMessage());
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
|
System.out.println("输入监听线程被中断");
|
|
|
|
|
|
Thread.currentThread().interrupt();
|
2025-07-31 14:44:10 +08:00
|
|
|
|
}
|
2025-08-23 13:56:54 +08:00
|
|
|
|
});
|
|
|
|
|
|
inputThread.setDaemon(true);
|
|
|
|
|
|
inputThread.start();
|
2025-07-31 14:44:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 数据处理线程:通过回车符判断扫码结束
|
|
|
|
|
|
*/
|
|
|
|
|
|
private static void startScanDataProcessor() {
|
|
|
|
|
|
Thread processorThread = new Thread(() -> {
|
|
|
|
|
|
StringBuilder inputBuffer = new StringBuilder();
|
2025-07-31 14:44:10 +08:00
|
|
|
|
|
2025-08-22 13:19:22 +08:00
|
|
|
|
try {
|
2025-08-23 13:56:54 +08:00
|
|
|
|
while (!isExit) {
|
|
|
|
|
|
// 取出队列中的字符
|
|
|
|
|
|
char c = inputQueue.take();
|
|
|
|
|
|
int ascii = (int) c;
|
|
|
|
|
|
|
|
|
|
|
|
// 判断是否为结束符(回车或换行)
|
|
|
|
|
|
if (ascii == ENTER_ASCII || ascii == NEWLINE_ASCII) {
|
|
|
|
|
|
// 缓冲区不为空时才处理(避免空输入)
|
|
|
|
|
|
if (inputBuffer.length() > 0) {
|
|
|
|
|
|
String scanInput = inputBuffer.toString().trim();
|
|
|
|
|
|
System.out.println("===== 扫码原始数据(完整) =====");
|
|
|
|
|
|
System.out.println(scanInput);
|
|
|
|
|
|
System.out.println("================================\n");
|
|
|
|
|
|
|
|
|
|
|
|
// 检查退出命令
|
|
|
|
|
|
if ("exit".equalsIgnoreCase(scanInput)) {
|
|
|
|
|
|
isExit = true;
|
|
|
|
|
|
System.out.println("\n已接收到退出指令,程序将关闭...");
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 校验JSON格式
|
|
|
|
|
|
if (isValidJson(scanInput)) {
|
|
|
|
|
|
System.out.println("JSON格式校验通过,开始处理...");
|
|
|
|
|
|
sendDataToServer(scanInput);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
System.out.println("错误:输入不是有效JSON格式\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 清空缓冲区,准备下一次扫码
|
|
|
|
|
|
inputBuffer.setLength(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 非结束符则添加到缓冲区
|
|
|
|
|
|
inputBuffer.append(c);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
|
System.out.println("数据处理线程被中断");
|
|
|
|
|
|
Thread.currentThread().interrupt();
|
2025-08-15 11:42:31 +08:00
|
|
|
|
}
|
2025-08-23 13:56:54 +08:00
|
|
|
|
});
|
|
|
|
|
|
processorThread.setDaemon(true);
|
|
|
|
|
|
processorThread.start();
|
2025-07-31 14:44:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 校验字符串是否为有效JSON
|
|
|
|
|
|
*/
|
|
|
|
|
|
private static boolean isValidJson(String input) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
new JSONObject(input);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
System.out.println("JSON解析失败原因: " + e.getMessage());
|
|
|
|
|
|
return false;
|
2025-07-31 14:44:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 锁定英文输入法(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' }\"";
|
2025-07-31 14:44:10 +08:00
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
Process process = Runtime.getRuntime().exec(powerShellCmd);
|
|
|
|
|
|
process.waitFor();
|
2025-08-15 11:42:31 +08:00
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
if (process.exitValue() == 0) {
|
|
|
|
|
|
System.out.println("英文输入法已锁定(en-US)");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
System.out.println("PowerShell切换输入法失败,尝试备选方案(Ctrl+空格)");
|
|
|
|
|
|
sendKeyStroke(0x11, 0x20);
|
2025-08-15 11:42:31 +08:00
|
|
|
|
}
|
2025-08-23 13:56:54 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
System.out.println("设置英文输入法异常: " + e.getMessage());
|
|
|
|
|
|
e.printStackTrace();
|
2025-07-31 14:44:10 +08:00
|
|
|
|
}
|
2025-08-23 13:56:54 +08:00
|
|
|
|
}
|
2025-07-31 14:44:10 +08:00
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 发送键盘组合键(用于输入法切换备选方案)
|
|
|
|
|
|
*/
|
|
|
|
|
|
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());
|
2025-07-31 14:44:10 +08:00
|
|
|
|
}
|
2025-08-23 13:56:54 +08:00
|
|
|
|
}
|
2025-07-31 14:44:10 +08:00
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 处理JSON数据并发送POST请求
|
|
|
|
|
|
*/
|
|
|
|
|
|
private static void sendDataToServer(String jsonInput) {
|
2025-07-31 14:44:10 +08:00
|
|
|
|
try {
|
2025-08-23 13:56:54 +08:00
|
|
|
|
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");
|
2025-08-15 11:42:31 +08:00
|
|
|
|
}
|
2025-08-23 13:56:54 +08:00
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
System.out.println("JSON处理异常: " + e.getMessage() + "\n");
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
System.out.println("发送请求异常: " + e.getMessage() + "\n");
|
2025-07-31 14:44:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-22 13:19:22 +08:00
|
|
|
|
|
2025-08-23 13:56:54 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 判断字符串是否为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("]"));
|
2025-08-22 13:19:22 +08:00
|
|
|
|
}
|
2025-07-31 14:44:10 +08:00
|
|
|
|
}
|