package com.hxzkoa.util;
|
|
import java.io.IOException;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import net.sf.json.JSONObject;
|
|
public class RequestUtils {
|
|
public static String getRequestJsonString(HttpServletRequest request,
|
String charset) throws IOException {
|
String submitMehtod = request.getMethod();
|
if (submitMehtod.equals("GET")) {
|
return new String(request.getQueryString().getBytes("UTF-8"), charset)
|
.replaceAll("%22", "\"");
|
} else {
|
byte buffer[] = getRequestPostBytes(request);
|
return new String(buffer, charset);
|
}
|
}
|
|
public static byte[] getRequestPostBytes(HttpServletRequest request)
|
throws IOException {
|
int contentLength = request.getContentLength();
|
if (contentLength < 0) {
|
return null;
|
}
|
byte buffer[] = new byte[contentLength];
|
for (int i = 0; i < contentLength;) {
|
|
int readlen = request.getInputStream().read(buffer, i,
|
contentLength - i);
|
if (readlen == -1) {
|
break;
|
}
|
i += readlen;
|
}
|
return buffer;
|
}
|
|
/**
|
* 使用统一的返回结果
|
*
|
* @param Status
|
* 返回结果状态 1.成功 0. 失败
|
* @param Message
|
* 返回结果描述信息
|
* @param ErrorCode
|
* 返回结果错误代码
|
* @param Data
|
* 数据结果,无返回则为null
|
* @return
|
*/
|
public static JSONObject returnJson(String status, String message,
|
String errorCode, String data) {
|
JSONObject reJson = new JSONObject();
|
reJson.put("Status", status);
|
reJson.put("Message", message);
|
reJson.put("ErrorCode", errorCode);
|
reJson.put("Describe", data);
|
return reJson;
|
}
|
}
|