How do I determine the content length of a request?
Author: Deron Eriksson
Description: This tutorial describes how to determine the content length of a request.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0) || Tomcat 5.5.20


A request header can contain a "content-length" header. If it is present, it reports the number of bytes present in the request when uploading a file or files. To illustrate this, I'll upload a 500+ megabyte file to a servletW, as shown here:

uploading large file to servlet

In the servlet, I'll display the request headers using the following code snippet:


...
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("Headers<hr/>");
		Enumeration<String> headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String headerName = headerNames.nextElement();
			out.print("Header Name: <em>" + headerName);
			String headerValue = request.getHeader(headerName);
			out.print("</em>, Header Value: <em>" + headerValue);
			out.println("</em><br/>");
		}
...

The servlet displays the following output in response to the 500+ megabyte file upload:

servlet reports request headers including content-length

Above, notice the "content-length" header, which displays the content-length of the request. It is enormous due to the 500+ megabyte file upload. So, to display the content length of the request, we can call request.getHeader("content-length"), as shown here:

...
	out.println("request content-length: " + request.getHeader("content-length"));
...

If we upload the file again with the above code snippet in the servlet, we see the following servlet output, which displays the content-length in bytes of the request:

diplaying content-length

There is no guarantee that the content-length header will be there, so you should always write your code to take this into account.

The ContentLengthServlet class contains the servlet code snippets mentioned above.

ContentLengthServlet.java

package test;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ContentLengthServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("Headers<hr/>");
		Enumeration<String> headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String headerName = headerNames.nextElement();
			out.print("Header Name: <em>" + headerName);
			String headerValue = request.getHeader(headerName);
			out.print("</em>, Header Value: <em>" + headerValue);
			out.println("</em><br/>");
		}

		out.println("<hr/>");

		out.println("request content-length: " + request.getHeader("content-length"));

	}
}