1 回答
TA贡献1842条经验 获得超12个赞
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
@Controller
public class TestController {
@RequestMapping(produces="text/plain;charset=UTF-8",value = "print")
@ResponseBody
public String print(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
try {
InputStream inputStream = request.getInputStream();
byte[] bytes = IOUtils.toByteArray(inputStream);
String result = new String(bytes, "UTF-8");
// System.out.println(result);
JSONObject json = (JSONObject) JSONObject.parse(result);
String name = json.getString("username");
if (name.equals("aaa")) {
map.put("us", "success");
} else {
map.put("us", "fail");
}
// System.err.println(name);
} catch (Exception e) {
e.printStackTrace();
}
return JSON.toJSONString(map);
}
}
添加回答
举报