10,606
社区成员
发帖
与我相关
我的任务
分享
@RequestMapping("/receiveData")
@ResponseBody
public String receiveData(@RequestParam HashMap<String, Object> parameterMap){
String json_str = parameterMap.get("data")==null?"":parameterMap.get("data").toString();
try {
String url = targetURL;
URL targetUrl = new URL(targetURL);
HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
httpConnection.setUseCaches(false);
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("DEPT_NAME", "测试");
json.put("FIELD_2_1", "测试1");
json.put("FIELD_2_2", "测试2");
json.put("FIELD_2_3", "测试3");
String param = URLEncoder.encode(json.toString(),"utf-8");
OutputStream outputStream = httpConnection.getOutputStream();
outputStream.write(param.getBytes());
outputStream.flush();
if (httpConnection.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + httpConnection.getResponseCode());
}
BufferedReader responseBuffer = new BufferedReader(
new InputStreamReader((httpConnection.getInputStream())));
String output;
System.out.println("Output from Server:\n");
while ((output = responseBuffer.readLine()) != null) {
System.out.println(output);
}
httpConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}