package com.ruoyi.buss.common;
|
|
import com.alibaba.fastjson2.JSONObject;
|
import com.ruoyi.buss.domain.vo.RfIdVo;
|
|
import java.io.*;
|
import java.net.ConnectException;
|
import java.net.HttpURLConnection;
|
import java.net.SocketTimeoutException;
|
import java.net.URL;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.Executors;
|
import java.util.concurrent.Future;
|
|
public class RfidUtil {
|
|
public static void main(String[] args) {
|
String result = sendJsonPost("http://192.168.0.2:10800/open/goods/task","{\"id\":12,\"name\":\"任务名称\", \"path\": \"路径信息\"}");
|
List<RfIdVo> list = new ArrayList<>();
|
list.add(new RfIdVo(1, "NAME1", "PATH1"));
|
list.add(new RfIdVo(2, "NAME2", "PATH2"));
|
list.add(new RfIdVo(3, "NAME3", "PATH3"));
|
list.add(new RfIdVo(4, "NAME4", "PATH4"));
|
list.add(new RfIdVo(5, "NAME5", "PATH5"));
|
list.add(new RfIdVo(6, "NAME6", "PATH6"));
|
|
System.out.println("正常" + result);
|
}
|
|
/**
|
* 异步发送请求
|
* @param url
|
* @param list
|
*/
|
public static void asyncSendJsonPost(String url, List<RfIdVo> list){
|
ExecutorService executor = Executors.newFixedThreadPool(50); // 根据网络调整线程数
|
List<Future<?>> futures = new ArrayList<>();
|
|
for (int i = 0, len = list.size(); i < len; i++) {
|
int finalI = i;
|
futures.add(executor.submit(() -> {
|
try {
|
URL realUrl = new URL(url);
|
HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
|
conn.setRequestMethod("POST");
|
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
conn.setRequestProperty("Accept", "*/*");
|
conn.setConnectTimeout(3000); // 3秒超时
|
conn.setDoOutput(true);
|
conn.setDoInput(true);
|
try(OutputStream os = conn.getOutputStream()){
|
byte[] input = JSONObject.toJSONString(list.get(finalI)).getBytes("utf-8");
|
os.write(input, 0, input.length);
|
}
|
conn.getResponseCode();
|
} catch (Exception ignored) {
|
System.out.println("发送RFID任务失败,请重试。"+ JSONObject.toJSONString(list.get(finalI)));
|
}
|
}));
|
}
|
executor.shutdown();
|
}
|
|
|
/**
|
* 向指定 URL 发送POST方法的请求
|
*
|
* @param url 发送请求的 URL
|
* @return 所代表远程资源的响应结果
|
*/
|
public static String sendJsonPost(String url, String jsonString) {
|
PrintWriter out = null;
|
BufferedReader in = null;
|
StringBuilder result = new StringBuilder();
|
try
|
{
|
URL realUrl = new URL(url);
|
HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
|
|
conn.setRequestMethod("POST");
|
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
conn.setRequestProperty("Accept", "*/*");
|
conn.setDoOutput(true);
|
conn.setDoInput(true);
|
try(OutputStream os = conn.getOutputStream()){
|
byte[] input = jsonString.getBytes("utf-8");
|
os.write(input, 0, input.length);
|
}
|
|
int responseCode = conn.getResponseCode();
|
if(responseCode == HttpURLConnection.HTTP_OK){
|
try(BufferedReader br = new BufferedReader(
|
new InputStreamReader(conn.getInputStream(), "utf-8")
|
)){
|
String responseLine;
|
while ((responseLine = br.readLine()) != null){
|
result.append(responseLine.trim());
|
}
|
System.out.println(result.toString());
|
}
|
}
|
conn.disconnect();
|
System.out.println("result=" + result);
|
}
|
catch (ConnectException e)
|
{
|
System.out.println("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + jsonString);
|
e.printStackTrace();
|
}
|
catch (SocketTimeoutException e)
|
{
|
System.out.println("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + jsonString);
|
e.printStackTrace();
|
}
|
catch (IOException e)
|
{
|
System.out.println("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + jsonString);
|
e.printStackTrace();
|
}
|
catch (Exception e)
|
{
|
System.out.println("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + jsonString);
|
e.printStackTrace();
|
}
|
finally
|
{
|
try
|
{
|
if (out != null)
|
{
|
out.close();
|
}
|
if (in != null)
|
{
|
in.close();
|
}
|
}
|
catch (IOException ex)
|
{
|
System.out.println("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + jsonString);
|
ex.printStackTrace();
|
}
|
}
|
return result.toString();
|
}
|
|
}
|