13693261870
2025-07-02 6708810c4de34dfb9513061432d656f91d56ee3a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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();
    }
 
}