0

I'm trying to post 2 fields, id and data, to a servlet using HttpClient. The problem is that if the length of the data field is less than 1MB or so, the servlet will get what I posted. But if the length of the data field is larger than 1MB or so, the servlet will receive null for all fields. What am I missing here? Thanks.

Here's the sample data that I post to the servlet:

id=12312123123123

data=the content of a file that is base-64 encoded

Here's the method that I use to post data to the servlet.

    private byte[] post(String aUrl,
                        Map<String,String> aParams,
                        String aCharsetEnc,
                        int aMaxWaitMs) throws Exception
{
    PostMethod post = null;
    try
    {
        HttpClient client = new HttpClient();
        post = new PostMethod(aUrl);
        post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=" + aCharsetEnc);           
                   
        for (String key : aParams.keySet())
        {
            post.addParameter(key, aParams.get(key));
        }

        final int code = client.executeMethod(post);
        if (code == HttpStatus.SC_NO_CONTENT || code == HttpStatus.SC_NOT_FOUND)
        {
            return null;
        }
        else if (code != HttpStatus.SC_OK)
        {
            throw new HttpException("Error code " + code + " encountered.");
        }

        InputStream stream = post.getResponseBodyAsStream();
        if (stream != null)
        {
            return BlobHelper.readBytes(stream);
        }
        return null;
    }
    finally
    {
        if (post != null)
        {
            post.releaseConnection();
        }
    }
}

Here's the method of the servlet.

public void doPost(HttpServletRequest aReq, HttpServletResponse aResp)
    throws ServletException, IOException
{
    setNoCache(aResp);
    aResp.setContentType("text/plain");

    try
    {
        final String id = aReq.getParameter(PARAM_ID);
        final String dataStr = aReq.getParameter(PARAM_DATA);


        if (log().isDebugEnabled())
        {
            log().debug("id=" + id);
            log().debug("data=" + dataStr);
        }
     }
     catch (Exception e)
     {
     }
}   
Community
  • 1
  • 1
user646073
  • 149
  • 5
  • 11

1 Answers1

1

Usually servlet containers have a maximum post size parameter.

For Tomcat you can follow the steps documented here(they should be similar for other appservers) -

Is there a max size for POST parameter content?

Community
  • 1
  • 1
Pushkar
  • 6,990
  • 9
  • 35
  • 57