@RequestMapping("pushdata")
public static String sub(String ip) throws IOException {
Map<String, String> querys = new HashMap<String, String>();
querys.put("data", "{\"id\":\"3705191106774\",\"scanType\":\"收件\",\"employeeId\":\"5100696004\",\"employeeName\":\"王小二\"," +
"\"employeeMobile\":\"020-28102036\",\"uploadSiteId\":\"dd31f041\",\"uploadSiteName\":\"广东广州番禺祈福分\"}");
try {
sendPost("http://" + ip + ":8080/express/expresspush/postdata", querys, "utf-8");
return "success";
}catch (Exception e){
e.printStackTrace();
}
return "error";
}
public static String sendPost(String urlParam, Map<String, String> params, String charset) {
StringBuffer resultBuffer = null;
// 构建请求参数
StringBuffer sbParams = new StringBuffer();
if (params != null && params.size() > 0) {
for (Map.Entry<String, String> e : params.entrySet()) {
sbParams.append(e.getKey());
sbParams.append("=");
sbParams.append(e.getValue());
sbParams.append("&");
}
}
URLConnection con = null;
OutputStreamWriter osw = null;
BufferedReader br = null;
try {
URL realUrl = new URL(urlParam);
// 打开和URL之间的连接
con = realUrl.openConnection();
// 设置通用的请求属性
con.setRequestProperty("accept", "*/*");
con.setRequestProperty("connection", "Keep-Alive");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
con.setDoOutput(true);
con.setDoInput(true);
// 获取URLConnection对象对应的输出流
osw = new OutputStreamWriter(con.getOutputStream(), charset);
if (sbParams != null && sbParams.length() > 0) {
// 发送请求参数
osw.write(sbParams.substring(0, sbParams.length() - 1));
// flush输出流的缓冲
osw.flush();
}
// 定义BufferedReader输入流来读取URL的响应
resultBuffer = new StringBuffer();
int contentLength = Integer.parseInt(con.getHeaderField("Content-Length"));
if (contentLength > 0) {
br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));
String temp;
while ((temp = br.readLine()) != null) {
resultBuffer.append(temp);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
osw = null;
throw new RuntimeException(e);
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
br = null;
throw new RuntimeException(e);
}
}
}
return resultBuffer.toString();
}
访问地址http://localhost:8080/express/expressInfoDemo/pushdata?ip=192.168.88.00 写你的ip
我刚做的小程序哦,欢迎交流程序问题哦~