美文网首页
Spring Boot 上传文件报The field file

Spring Boot 上传文件报The field file

作者: 是瑞瀛呀 | 来源:发表于2019-03-29 14:40 被阅读0次

    根据异常内提示的类,可以定位到:
    FileUploadBase(class FileItemStreamImpl 的构造函数)

    /**
                 * Creates a new instance.
                 *
                 * @param pName The items file name, or null.
                 * @param pFieldName The items field name.
                 * @param pContentType The items content type, or null.
                 * @param pFormField Whether the item is a form field.
                 * @param pContentLength The items content length, if known, or -1
                 * @throws IOException Creating the file item failed.
                 */
                FileItemStreamImpl(String pName, String pFieldName,
                        String pContentType, boolean pFormField,
                        long pContentLength) throws IOException {
                    name = pName;
                    fieldName = pFieldName;
                    contentType = pContentType;
                    formField = pFormField;
                    final ItemInputStream itemStream = multi.newInputStream();
                    InputStream istream = itemStream;
                    if (fileSizeMax != -1) {
                        if (pContentLength != -1
                                &&  pContentLength > fileSizeMax) {
                            FileSizeLimitExceededException e =
                                new FileSizeLimitExceededException(
                                    String.format("The field %s exceeds its maximum permitted size of %s bytes.",
                                            fieldName, Long.valueOf(fileSizeMax)),
                                    pContentLength, fileSizeMax);
                            e.setFileName(pName);
                            e.setFieldName(pFieldName);
                            throw new FileUploadIOException(e);
                        }
                        istream = new LimitedInputStream(istream, fileSizeMax) {
                            @Override
                            protected void raiseError(long pSizeMax, long pCount)
                                    throws IOException {
                                itemStream.close(true);
                                FileSizeLimitExceededException e =
                                    new FileSizeLimitExceededException(
                                        String.format("The field %s exceeds its maximum permitted size of %s bytes.",
                                               fieldName, Long.valueOf(pSizeMax)),
                                        pCount, pSizeMax);
                                e.setFieldName(fieldName);
                                e.setFileName(name);
                                throw new FileUploadIOException(e);
                            }
                        };
                    }
                    stream = istream;
                }
    

    发现fileSizeMax该值控制上传上限,在207行看到

        /**
         * Sets the maximum allowed size of a single uploaded file,
         * as opposed to {@link #getSizeMax()}.
         *
         * @see #getFileSizeMax()
         * @param fileSizeMax Maximum size of a single uploaded file.
         */
        public void setFileSizeMax(long fileSizeMax) {
            this.fileSizeMax = fileSizeMax;
        }
    

    追踪后发现在Request类的parseParts方法(2863行)upload.setFileSizeMax(mce.getMaxFileSize());

    mce对象是在2800行MultipartConfigElement mce = getWrapper().getMultipartConfigElement();初始化的。
    通过getMultipartConfigElement()可在StandardWrapper内的1072行处发现如下代码

    if (multipartConfigElement == null) {
                    MultipartConfig annotation =
                            servlet.getClass().getAnnotation(MultipartConfig.class);
                    if (annotation != null) {
                        multipartConfigElement =
                                new MultipartConfigElement(annotation);
                    }
                }
    

    最终发现 通过MultipartConfig注解可配置。
    配置完不好使!!!
    后发现可通过配置文件:

    spring:
      http:
        multipart:
          maxFileSize: 10Mb
          maxRequestSize: 100Mb
    

    相关文章

      网友评论

          本文标题:Spring Boot 上传文件报The field file

          本文链接:https://www.haomeiwen.com/subject/nddlbqtx.html