救急!action中request的数据为空,报错String index out of range: -1

lllcool lllcool 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
public void write() throws Exception {
// TODO Auto-generated method stub

HttpServletRequest request = ServletActionContext.getRequest();;
HttpServletResponse response = ServletActionContext.getResponse();
// httpURLConnection.setRequestProperty("content-type", "text/html");
response.setContentType("text/html;charset=UTF-8");
InputStream inputStream = request.getInputStream();
// byte[] b = new BufferedReader(new InputStreamReader(inputStream)).readLine().getBytes();
byte[] b = IOUtils.toByteArray(inputStream);
System.out.println("cuowubaogao1 :"+b.length);//测试1
String str = new String(b,"UTF-8");
System.out.println("cuowubaogao2 :"+str.length());//测试2
str = str.substring(str.indexOf("{"),str.lastIndexOf("}")+1);
JSONObject j = (JSONObject) JSONObject.parse(str);
username = j.getString("username");
userpassword = j.getString("userpassword");
UserTable u = new UserTable();
u.setUsername(username);
u.setPassword(userpassword);
u.setPower(0);

UserTableDao ud = new UserTableDao();

PrintWriter pw = response.getWriter();
if(ud.queryUser(username)!=null) {
j = new JSONObject();
j.put("result", "user_error");
} else if(ud.insertUser(u)) {
j = new JSONObject();
j.put("result", "success");
} else {
j = new JSONObject();
j.put("result", "error");
}
pw.write(j.toJSONString());
pw.flush();
pw.close();


测试1,2的长度都显示为0
信息: Server startup in 20104 ms
cuowubaogao1 :0
cuowubaogao2 :0
...全文
评论 4
收藏 已收藏
分享
返回顶部
lllcool
Java EE 67,535 成员 225,843 内容