java - HttpServletRequest 读取大型帖子正文 - 无法读取所有数据,等待所有 block

标签 java apache http tomcat servlets

当我这样读数据的时候

    String body = null;

    try {
        body = IOUtils.toString(request.getReader());
    } catch (IOException e) {
        //
    }

并非所有数据都被读取。 “content-length” header 包含正确的数据长度,但看起来 IOUtils.toString 没有等待所有 block 。

这个旧的冗长代码有效,但我想知道是否有一种现代方法可以等待所有数据被读取:

    String body = null; 
            try {
                ServletInputStream in = request.getInputStream();
                String reqLengthString = request.getHeader("content-length");
                int requestLength = 0;

                try {
                    requestLength = Integer.valueOf(reqLengthString).intValue();
                } catch (Exception e) {
                    return doForecastErrorResponse(response,"Invalid Content Length");
                }
                Debug.logInfo("RequestLength: "+requestLength, MODULE);

                int i = -1;
                while (requestLength > 0) {
                    byte[] line = new byte[8192];
                    i = waitingReadLine(in, line, 0, 8192, requestLength);
                    requestLength -= i;
                    if (Debug.verboseOn()) {
                        Debug.logVerbose("remaining request length: "+requestLength, MODULE);
                        Debug.logVerbose("data this grab: "+new String(line), MODULE);
                    }

                    jb.append(new String(line));
                }

                body = jb.toString().trim();
                if (Debug.verboseOn()) {
                    Debug.logVerbose("String: "+body, MODULE);
                }

            } catch (IOException e) {
                return doForecastErrorResponse(response,"Error reading input."+e.getMessage());

            }


private static int waitingReadLine(ServletInputStream in, byte[] buf, int off, int len, int reqLen) throws IOException {
        int i = -1;

        while (((i = in.readLine(buf, off, len)) == -1) && (reqLen > 0)) {
            int waitCount = 0;
            int MAX_WAITS = 30;
            int WAIT_INTERVAL = 1000;

            if (Debug.verboseOn()) Debug.logVerbose("Waiting for read line", MODULE);
            if (waitCount > MAX_WAITS) {
                if (Debug.verboseOn()) Debug.logVerbose("Waited " + waitCount + " times, bailing out while still expecting " + reqLen + " bytes.", MODULE);
                throw new IOException("waited " + waitCount + " times, bailing out while still expecting " +
                        reqLen + " bytes.");
            }
            waitCount++;
            long endMS = new Date().getTime() + WAIT_INTERVAL;

            while (endMS > (new Date().getTime())) {
                try {
                    Thread.sleep(WAIT_INTERVAL);
                } catch (Exception e3) {
                    if (Debug.verboseOn()) Debug.logVerbose("Exception waiting for read line"+ e3.getMessage(), MODULE);
                }
            }
            if (Debug.verboseOn()) Debug.logVerbose("Waited " + (new Date().getTime() - (endMS - WAIT_INTERVAL)) + " ms", MODULE);
        }
        return i;
    }

最佳答案

更新:实际上诀窍是使用 getInputStream 而不是 getReader:

IOUtils.toString(request.getInputStream());

发布来自@John Bollinger 的评论的答案

    byte[] buffer = new byte[requestLength];
    IOUtils.readFully(request.getInputStream(), buffer);
    body = new String(buffer);

关于java - HttpServletRequest 读取大型帖子正文 - 无法读取所有数据,等待所有 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54992990/

相关文章:

apache - 有没有办法让 Apache 提供名称中带有问号的文件?

http - 如何限制用 golang 编写的 Web 服务器允许特定地址而不是模式?

javascript - 更新组件以反射(reflect)数据变化?

apache - 无效命令 'Mutex'

HTTPS GET 调用适用于第 3 方 API 上的浏览​​器,但不适用于 curl/fuel

Java Reflection,更改私有(private)静态最终字段没有做任何事情

java - 重复资源 Wildfly

java - 如何在 FXML 中使用预定义的 Spinner 样式类

java - 在 Java 中创建 .vsdx 文件 (Microsoft Visio)

apache - 如何构建 apache HCatalog 0.5.0?