本文整理匯總了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()
);
}