Java Code Examples for javax.servlet.http.HttpServletRequest.getContentLength()

The following are Jave code examples for showing how to use getContentLength() of the javax.servlet.http.HttpServletRequest class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: stage-job   File: HttpClientUtil.java   View Source Code Vote up 7 votes
/**
 * 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[] {};
}
 
Example 2
Project: netty_op   File: JettyServerHandler.java   View Source Code Vote up 6 votes
/**
 *@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[] {};
}
 
Example 3
Project: xxl-job   File: HttpClientUtil.java   View Source Code Vote up 6 votes
/**
 * 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[] {};
}
 
Example 4
Project: k8s-proxy   File: K8sReverseProxy.java   View Source Code Vote up 5 votes
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);
}
 
Example 5
Project: parabuild-ci   File: HttpServletRequestTest.java   View Source Code Vote up 5 votes
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 ) );
}
 
Example 6
Project: gerrit-plugin   File: GerritWebHook.java   View Source Code Vote up 5 votes
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);
  }
}
 
Example 7
Project: plumdo-stock   File: JsonContentCachingRequestWrapper.java   View Source Code Vote up 4 votes
public JsonContentCachingRequestWrapper(HttpServletRequest request) {
	super(request);
	int contentLength = request.getContentLength();
	this.cachedContent = new ByteArrayOutputStream(contentLength >= 0 ? contentLength : 1024);
}
 
Example 8
Project: convertigo-engine   File: ProxyServletRequester.java   View Source Code Vote up 4 votes
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()
		);
	}