传入参数类型 {
"aaa":"aaaa",
"bbb":"bbb",
"ccc":"ccc",
"waterMark":[{'name':'默认name1','position':'默认地址1'},{'name':'默认name2','position':'默认地址2'}]
}
遍历
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xxx.live.core.util.others.ConfigUtil;
import com.xxxx.live.webapp.util.HttpClientUtil;
import com.xxxx.mobilevideo.core.util.StringUtil;
/**
* Servlet implementation class CloudCutStreamServlet
*/
public class CloudCutStreamServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Logger log = Logger.getLogger(RecordFinishServlet.class);
/**
* @see HttpServlet#HttpServlet()
*/
public CloudCutStreamServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(req, resp);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//1.校验参数
JSONObject resultMap = this.getResutltMap(req);
if (null == resultMap) {
response(resp, getMsg(false, "请求参数格式不正确", "10002"));
return;
}
String liveName = resultMap.getString("liveName");
String masterUrl = resultMap.getString("masterUrl");
String slaveUrl = resultMap.getString("slaveUrl"); // 可以为空
String reason=resultMap.getString("reason"); // 切流原因不可以为空
JSONArray waterMark=resultMap.getJSONArray("waterMark"); //接收对象里的数组
//判断水印是否为空,为空则使用默认水印,
if(waterMark.isEmpty()){
//使用默认的水印
String waterMarkDefaultValue="[{'name':'默认name1','position':'默认地址1'},{'name':'默认name2','position':'默认地址2'}]";
resultMap.put("waterMark", waterMarkDefaultValue);
}else{
//水印不为空判断数组中的json的key值是否为空
int jsonSize = waterMark.size(); //获取数组长度
for(int i=0;i<jsonSize;i++){
JSONObject jsonTemp = (JSONObject)waterMark.getJSONObject(i);
if(StringUtil.isNullStr(jsonTemp.getString("name"))||StringUtil.isNullStr(jsonTemp.getString("position"))){
response(resp, getMsg(false, "参数为空", "10001"));
return;
}
}
}
// 接受参数校验
if (StringUtil.isNullStr(liveName) || StringUtil.isNullStr(masterUrl)||StringUtil.isNullStr(reason)) {
response(resp, getMsg(false, "参数为空", "10001"));
return;
}
//参数正常
try {
String result ="";
String change_source_url = ConfigUtil.getProperty("change_source_Url");
response(resp, getMsg(true, "请求成功", "99999"));
} catch (Exception e) {
e.printStackTrace();
}
}
public JSONObject getResutltMap(HttpServletRequest req) {
String requestStr = "";
StringBuilder sb = new StringBuilder();
InputStream is = null;
BufferedReader dis = null;
try {
is = req.getInputStream();
dis = new BufferedReader(new InputStreamReader(is, "utf-8"));
String str = "";
while ((str = dis.readLine()) != null) {
sb.append(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null) {
dis.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
requestStr = sb.toString();
try {
JSONObject json = JSON.parseObject(requestStr);
return json;
} catch (Exception e) {
}
return null;
}
private String getMsg(boolean success, String msg, String errCode) {
Map<String, Object> returnMap = new HashMap<String, Object>();
returnMap.put("success", success);
returnMap.put("msg", msg);
returnMap.put("errCode", errCode);
String returnJson = JSON.toJSONString(returnMap);
return returnJson;
}
private void response(HttpServletResponse resp, String msg) {
try {
resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8");
PrintWriter out = resp.getWriter();
out.println(msg);
out.close();
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}