本文整理汇总了Java中javax.servlet.http.HttpServletRequest.getContentLength方法的典型用法代码示例。如果您正苦于以下问题:Java HttpServletRequest.getContentLength方法的具体用法?Java HttpServletRequest.getContentLength怎么用?Java HttpServletRequest.getContentLength使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.http.HttpServletRequest
的用法示例。
在下文中一共展示了HttpServletRequest.getContentLength方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readRequestPayLoadBytes
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
*@description 读取Post请求中的有效载荷对象
*@time 创建时间:2017年12月14日上午11:31:40
*@param request
*@return
*@throws IOException
*@author dzn
*/
public static final byte[] readRequestPayLoadBytes(HttpServletRequest request) throws IOException {
request.setCharacterEncoding("UTF-8");
int contentLen = request.getContentLength();
InputStream is = request.getInputStream();
if (contentLen > 0) {
int readLen = 0;
int readLengthThisTime = 0;
byte[] message = new byte[contentLen];
try {
while (readLen != contentLen) {
readLengthThisTime = is.read(message, readLen, contentLen - readLen);
if (readLengthThisTime == -1) {
break;
}
readLen += readLengthThisTime;
}
return message;
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
return new byte[] {};
}
示例2: readBytes
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
* read bytes from http request
* @param request
* @return
* @throws IOException
*/
public static final byte[] readBytes(HttpServletRequest request) throws IOException {
request.setCharacterEncoding("UTF-8");
int contentLen = request.getContentLength();
InputStream is = request.getInputStream();
if (contentLen > 0) {
int readLen = 0;
int readLengthThisTime = 0;
byte[] message = new byte[contentLen];
try {
while (readLen != contentLen) {
readLengthThisTime = is.read(message, readLen, contentLen - readLen);
if (readLengthThisTime == -1) {
break;
}
readLen += readLengthThisTime;
}
return message;
} catch (IOException e) {
logger.error(e.getMessage(), e);
throw e;
}
}
return new byte[] {};
}
示例3: readBytes
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
* read bytes from http request
* @param request
* @return
* @throws IOException
*/
public static final byte[] readBytes(HttpServletRequest request) throws IOException {
request.setCharacterEncoding("UTF-8");
int contentLen = request.getContentLength();
InputStream is = request.getInputStream();
if (contentLen > 0) {
int readLen = 0;
int readLengthThisTime = 0;
byte[] message = new byte[contentLen];
try {
while (readLen != contentLen) {
readLengthThisTime = is.read(message, readLen, contentLen - readLen);
if (readLengthThisTime == -1) {
break;
}
readLen += readLengthThisTime;
}
return message;
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
return new byte[] {};
}
示例4: getInputStreamEntity
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
private InputStreamEntity getInputStreamEntity(HttpServletRequest request) throws IOException
{
int contentLength = request.getContentLength();
ContentType contentType = null;
if (request.getContentType() != null) {
contentType = ContentType.parse(request.getContentType());
}
return new InputStreamEntity(request.getInputStream(), contentLength, contentType);
}
示例5: testRequestMessageBody
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
public void testRequestMessageBody() throws Exception {
String body = "12345678901234567890";
InputStream stream = new ByteArrayInputStream( body.getBytes( "UTF-8" ) );
WebRequest wr = new PutMethodWebRequest( "http://localhost/simple", stream, "text/plain" );
HttpServletRequest request = new ServletUnitHttpRequest( NULL_SERVLET_REQUEST, wr, new ServletUnitContext(), new Hashtable(), body.getBytes() );
assertEquals( "Request content length", body.length(), request.getContentLength() );
BufferedInputStream bis = new BufferedInputStream( request.getInputStream() );
byte[] buffer = new byte[ request.getContentLength() ];
bis.read( buffer );
assertEquals( "Request content", body, new String( buffer ) );
}
示例6: getBody
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
private GerritProjectEvent getBody(HttpServletRequest req) throws IOException {
char[] body = new char[req.getContentLength()];
try (InputStreamReader is = new InputStreamReader(req.getInputStream())) {
IOUtils.readFully(is, body);
String bodyString = new String(body);
log.info("Received body: " + bodyString);
return gson.fromJson(bodyString, GerritProjectEvent.class);
}
}
示例7: JsonContentCachingRequestWrapper
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
public JsonContentCachingRequestWrapper(HttpServletRequest request) {
super(request);
int contentLength = request.getContentLength();
this.cachedContent = new ByteArrayOutputStream(contentLength >= 0 ? contentLength : 1024);
}
示例8: gatherRequestInfo
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
private void gatherRequestInfo(ParameterShuttle infoShuttle, HttpServletRequest request) throws IOException {
infoShuttle.sessionID = request.getSession().getId();
infoShuttle.postFromUser = request.getMethod().equals("POST");
if (infoShuttle.postFromUser) {
infoShuttle.userContentType = request.getContentType();
infoShuttle.userContentLength = request.getContentLength();
}
/* String query = request.getQueryString();
if (query != null) {
int pos = 0;
infoShuttle.postToSite = query.startsWith(ParameterShuttle.bridgePostTag);
if (infoShuttle.postToSite)
pos = ParameterShuttle.bridgePostTag.length();
if (query.startsWith(ParameterShuttle.bridgeGotoTag, pos)) {
int p2 = query.indexOf(ParameterShuttle.bridgeThenTag, pos);
if (p2 > -1) {
infoShuttle.userGoto = query.substring(pos + ParameterShuttle.bridgeGotoTag.length(), p2);
infoShuttle.userThen = query.substring(p2 + ParameterShuttle.bridgeThenTag.length());
}
else {
infoShuttle.userGoto = query.substring(pos + ParameterShuttle.bridgeGotoTag.length());
}
}
else if (query.startsWith(ParameterShuttle.bridgeThenTag, pos)) {
infoShuttle.userThen = query.substring(pos + ParameterShuttle.bridgeThenTag.length());
}
}*/
infoShuttle.postToSite = infoShuttle.postFromUser;//request.getParameter(Parameter.ProxyPost.getName()) != null;
infoShuttle.userGoto = request.getParameter(Parameter.ProxyGoto.getName());
infoShuttle.userThen = request.getParameter(Parameter.ProxyThen.getName());
Enumeration<String> allHeaderNames = GenericUtils.cast(request.getHeaderNames());
while (allHeaderNames.hasMoreElements()) {
String name = (String) allHeaderNames.nextElement();
infoShuttle.userHeaderNames.add(name.toLowerCase());
infoShuttle.userHeaderValues.add(request.getHeader(name));
}
ParameterShuttle.getSelfURL(
request.getScheme(),
request.getServerName(),
request.getServerPort(),
request.getRequestURI()
);
}