原代码:
JSONObject jsonObject = null;
OutputStream os = null;
InputStream is = null;
try {
String msg = null;
os = new ByteArrayOutputStream();
is = req.getInputStream();
if (is != null) {
byte[] b = new byte[1024];
int len = 0;
while ((len = is.read(b)) != -1) {
os.write(b,0,len);
}
msg = os.toString();
}
String result = new String(msg.getBytes(Charset.defaultCharset()), "UTF-8");
jsonObject = new JSONObject(result);
} catch (Exception e) {
LogUtil.error(e);
} finally {
try {
is.close();
os.close();
} catch (IOException e) {
LogUtil.error(e);
}
}
修改为:
JSONObject jsonObject = null;
BufferedReader br = null;
try {
String msg = null;
br = new BufferedReader(new InputStreamReader(req.getInputStream(),"UTF-8"));
String line = null;
StringBuilder sb = new StringBuilder();
while((line = br.readLine())!=null){
sb.append(line);
}
br.close();
msg = sb.toString();
//LogUtil.info("msg:"+msg);
jsonObject = new JSONObject(msg);
} catch (Exception e) {
LogUtil.error(e);
} finally {
try {
br.close();
} catch (IOException e) {
LogUtil.error(e);
}
}