首页 > 解决方案 > 为什么使用“multipart/form-data”上传文件时经常出现异常?

问题描述

我在form中使用enctype="multipart/form-data"提交文件。但是在没有文件上传的情况下,经常报异常。

org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest.warn:68 - Unable to parse request
org.apache.commons.fileupload.FileUploadException: early EOF
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:362)
Caused by: org.eclipse.jetty.io.EofException: early EOF
    at org.eclipse.jetty.server.HttpInput.read(HttpInput.java:65)
    at java.io.FilterInputStream.read(FilterInputStream.java:133)
    at org.apache.commons.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:134)
    at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:999)

我调试 jar,发现 MultipartStream.skipPreamble() 中抛出了异常

public boolean skipPreamble() throws IOException {
        // First delimiter may be not preceeded with a CRLF.
        System.arraycopy(boundary, 2, boundary, 0, boundary.length - 2);
        boundaryLength = boundary.length - 2;
        try {
            // Discard all data up to the delimiter.
            discardBodyData();

            // Read boundary - if succeeded, the stream contains an
            // encapsulation.
            return readBoundary();
        } catch (MalformedStreamException e) {
            return false;
        } finally {
            // Restore delimiter.
            System.arraycopy(boundary, 0, boundary, 2, boundary.length - 2);
            boundaryLength = boundary.length;
            boundary[0] = CR;
            boundary[1] = LF; //here!!!!this line will throw a IOException
        }
    }

我很困惑,但我无法稳定地重现它,因为问题存在,然后它不存在。有没有人遇到过这个问题?

标签: javastruts2

解决方案


推荐阅读