首页 > 解决方案 > 输入流以 -1 开头

问题描述

当使用我的 CXF REST API 以慢速连接(仅)上传文件时,Couldn't find MIME boundary出现错误。于是我调试了CXF核心代码来找出原因。现在我正在查看这个 CXF 核心代码[1]。

    private static boolean readTillFirstBoundary(PushbackInputStream pbs, byte[] bp) throws IOException {

        // work around a bug in PushBackInputStream where the buffer isn't
        // initialized
        // and available always returns 0.
        int value = pbs.read();
        pbs.unread(value);
        while (value != -1) {
            value = pbs.read();

当客户端到服务器的连接速度很慢时,输入流的第一个值几乎总是-1. 这会Couldn't find MIME boundary在流程的后期导致错误。

如果我更改代码以跳过第一个字节(如果它是 -1 如下所示),则它可以顺利运行。

    private static boolean readTillFirstBoundary(PushbackInputStream pbs, byte[] bp) throws IOException {

        // work around a bug in PushBackInputStream where the buffer isn't
        // initialized
        // and available always returns 0.
        int value = pbs.read();
        if (value == -1) {                <<<<<< if the first byte is -1,
            value = pbs.read();           <<<<<< ignore that and read the  
        }                                 <<<<<< next byte
        pbs.unread(value);
        while (value != -1) {
            value = pbs.read();

知道可能是什么原因吗?

[1] https://github.com/apache/cxf/blob/cxf-3.2.8/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java#L264

标签: javacxfinputstreamattachment

解决方案


原来是一个 tomcat 错误[1]。:-/

适用于更高版本的 Tomcat 的文件。

[1] https://bz.apache.org/bugzilla/show_bug.cgi?id=64195


推荐阅读