打包后调用出错(乱码问题)

This commit is contained in:
2025-02-25 11:32:36 +08:00
parent aec28f662a
commit 787892924b
3 changed files with 102 additions and 17 deletions

View File

@@ -40,7 +40,9 @@ public class ThirdServiceUtil {
*/
public <T> T callDevice(LexMarkDTO lexMarkDTO, Class<T> responseType) {
try {
URL url = new URL(lexMarkServiceIp + ":" + lexMarkServicePort + "/CallDevice");
String urlStr = lexMarkServiceIp + ":" + lexMarkServicePort + "/CallDevice";
URL url = new URL(urlStr);
log.info("开始请求利盟服务URL: {}, 请求体: {}", urlStr, JSON.toJSONString(lexMarkDTO));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
@@ -52,23 +54,28 @@ public class ThirdServiceUtil {
wr.write(input, 0, input.length);
}
int responseCode = connection.getResponseCode();
log.info("利盟服务响应码: {}", responseCode);
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new RRException("利盟服务请求失败,响应码:" + responseCode);
}
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
// 指定编码为 UTF-8
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
String responseStr = response.toString();
log.info("利盟服务响应内容: {}", responseStr);
// 将响应 JSON 字符串转换为泛型对象
T result = JSON.parseObject(responseStr, responseType);
if (result instanceof LexMarkResultVO && ((LexMarkResultVO) result).getResult() != 0) {
throw new RRException(result.toString());
}
return result;
}
in.close();
// 将响应 JSON 字符串转换为泛型对象
T result = JSON.parseObject(response.toString(), responseType);
if (result instanceof LexMarkResultVO && ((LexMarkResultVO) result).getResult() != 0) {
throw new RRException(result.toString());
}
return result;
} catch (Exception e) {
log.error("利盟服务请求发生异常", e);
if (e instanceof RRException) {
throw new RRException(((RRException) e).getCode(), e.getMessage());
}