使用 httpclient 将数据发布到 servlet

发布于 2024-12-14 08:03:19 字数 2066 浏览 0 评论 0原文

我正在尝试使用 HttpClient 将 2 个字段(id 和 data)发布到 servlet。 问题是,如果数据字段的长度小于 1MB 左右,servlet 将获取我发布的内容。但是,如果数据字段的长度大于 1MB 左右,则 servlet 将收到所有字段的 null。我在这里缺少什么?谢谢。

以下是我发布到 servlet 的示例数据:

id=12312123123123

data=base-64 编码的文件内容

这是我用来将数据发布到 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();
        }
    }
}

这是 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)
     {
     }
}   

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)
     {
     }
}   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征﹌骨岁月お 2024-12-21 08:03:19

通常 servlet 容器有一个最大帖子大小参数。

对于 Tomcat,您可以按照此处记录的步骤进行操作(对于其他应用程序服务器,它们应该类似) -

POST 参数内容是否有最大大小?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文