MalformedStreamException:流意外结束

xyb*_*rek 3 java apache multipartform-data java-8

鉴于:

byteString

-----------------------------149742642616556
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain

test
-----------------------------149742642616556--
Run Code Online (Sandbox Code Playgroud)

然后这段代码(未优化):

Pattern pattern = Pattern.compile(BOUNDARY_PATTERN); // "(?m)\\A-+\\d+$"
Matcher matcher = pattern.matcher(byteString);
String boundary = null;
while (matcher.find()) {
    boundary = matcher.group();
    contentType = "multipart/form-data; boundary=" + boundary;
}
LOG.info("Content Type = " + contentType);

@SuppressWarnings("deprecation")
org.apache.commons.fileupload.MultipartStream multipartStream =
        new org.apache.commons.fileupload.MultipartStream(new ByteArrayInputStream(byteString.getBytes()), boundary.getBytes());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
multipartStream.readBodyData(bos); // throw error
byte[] byteBody = bos.toByteArray();
Run Code Online (Sandbox Code Playgroud)

抛出这个错误:

org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
    at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:1005)
    at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:903)
    at java.io.InputStream.read(InputStream.java:101)
    at org.apache.commons.fileupload.util.Streams.copy(Streams.java:100)
    at org.apache.commons.fileupload.util.Streams.copy(Streams.java:70)
    at org.apache.commons.fileupload.MultipartStream.readBodyData(MultipartStream.java:593)
Run Code Online (Sandbox Code Playgroud)

这里可能有什么问题?我很感激这里的帮助。

Ame*_*hel 6

问题似乎是由于线路末端错误和检索边界的方式。根据来自SO 答案RFC2046引用:

多部分实体的 Content-Type 字段需要一个参数“边界”。然后,边界定界符行被定义为完全由两个连字符(“-”,十进制值 45)组成的行后跟来自 Content-Type 标头字段的边界参数值、可选的线性空格和终止 CRLF

问题恰恰在于两点:线型末端和边界参数值前面的两个连字符

行尾

由于您的代码没有准确显示 byteString 的值,我尝试了LF ( \n) 和CRLF ( \r\n) 行尾以查看会发生什么。

错误的行尾(即不是 CRLF)恰好在最后一个边界之前时,似乎会重现该问题,如下所示:

String byteString=
    "-----------------------------149742642616556\r\n" +
    "Content-Disposition: form-data; name=\"file\"; filename=\"test.txt\"\r\n" +
    "Content-Type: text/plain; charset=UTF-8\r\n" +
    "\r\n" +
    "test\r\n" + // <-- only \n here lead to a MalformedStreamException
    "-----------------------------149742642616556--\r\n";
Run Code Online (Sandbox Code Playgroud)

听起来 MultipartStream 无法解析边界的开头,因为它没有在上一行中捕获行的右端 (CRLF)。所以,我你使用了 LF 终止符,你应该用 CRLF 终止符替换它们。

边界格式

RFC 告诉我们边界定界符是两个连字符 + 边界参数 + CRLF。您的正则表达式不仅捕获边界参数值,还包括两个连字符。所以我替换了这部分:

// capturing group = boundary parameter value
String regexp="(?m)\\A--(-*\\d+)$";
// [...]
while (matcher.find()) {
    boundary = matcher.group(1);
    // [...]
}
Run Code Online (Sandbox Code Playgroud)

工作代码

可作为 MCVE 运行

您将在下面找到的代码可以在没有 Tomcat 的控制台中运行。只需要commons-fileupload-1.3.3-bin.tar.gzcommons-io-2.6-bin.tar.gz

要查看 解析的内容MultipartStream,我在通话中暂时替换bos了(如评论中所述)。System.outreadBodyData()

代码本身

import java.util.regex.*;
import java.io.*;
import org.apache.commons.fileupload.*;

public class Test {
    public final static void main(String[] argv) {
    String byteString=
        "-----------------------------149742642616556\r\n" +
        "Content-Disposition: form-data; name=\"file\"; filename=\"test.txt\"\r\n" +
        "Content-Type: text/plain; charset=UTF-8\r\n" +
        "\r\n" +
        "test\r\n" + // <-- only \n here lead to a MalformedStreamException
        "-----------------------------149742642616556--\r\n";

    String regexp="(?m)\\A--(-*\\d+)$"; // edited regexp to catch the right boundary

    Pattern pattern = Pattern.compile(regexp);
    Matcher matcher = pattern.matcher(byteString);
    String boundary = null;
    String contentType=null;
    while (matcher.find()) {
        boundary = matcher.group(1);
        contentType = "multipart/form-data; boundary=\"" + boundary + "\"";
    }

    System.out.println("boundary = \"" + boundary + "\"");

    @SuppressWarnings("deprecation")
        org.apache.commons.fileupload.MultipartStream multipartStream =
        new org.apache.commons.fileupload.MultipartStream
        (new ByteArrayInputStream(byteString.getBytes()), boundary.getBytes());
     ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
        // Use the commented line instead the following one
        // To see what the multipartStream is reading (for debug)
        // multipartStream.readBodyData(System.out);
        multipartStream.readBodyData(bos);
    } catch (MultipartStream.MalformedStreamException e) {
        System.out.println("Malformed Exception " + e.getMessage());
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    byte[] byteBody = bos.toByteArray();

    // Displaying the body read
    for(byte c : byteBody) {
        System.out.format("%c", c);
    }
    System.out.println();
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

boundary = "---------------------------149742642616556"
-----------------------------149742642616556
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain; charset=UTF-8

test
Run Code Online (Sandbox Code Playgroud)