0

我正在尝试向服务器发送多部分请求,但出现以下异常

org.springframework.web.multipart.MultipartException: Current request is not a multipart request
    at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:188)
    at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:104)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)

基于教程和stackoverflow问题,我开发了以下代码

控制器

  @RequestMapping(value = "/upload-file", method = RequestMethod.POST)
    public ResponseEntity<Void> getUploadFile(@RequestParam("file") MultipartFile file) {
    LOGGER.debug(String.valueOf(file));}

多部分配置

@Configuration
@EnableWebMvc
public class MultipartConfiguration {

    private static final String DEFAULT_ENCODING = "UTF-8";

    private static final long MAX_UPLOAD_FILE_SIZE = 100000000;

    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver resolver=new CommonsMultipartResolver();
        resolver.setDefaultEncoding("utf-8");
        resolver.setMaxUploadSize(MAX_UPLOAD_FILE_SIZE);
        resolver.setDefaultEncoding(DEFAULT_ENCODING);
        return resolver;
    }
}

html

<input type="file"  style="display:none" #fileupload name="image" (change)="fileProgress($event)" />

零件

export class FileUploadComponent {
  fileData: any = null;
  uploadedFilePath: string = null;
   formData = new FormData();
    reader = new FileReader();
  @Output() fiLoaded: EventEmitter<File> = new EventEmitter();
  constructor(private pointeService: PointeService) { }

  fileProgress(fileInput: any) {

    this.fileData = fileInput.target.files[0];

    this.reader.readAsDataURL(this.fileData);
    this.reader.onloadend = (_event) => {
      this.formData.append('file', this.fileData);
      this.onFileLoaded(this.formData);
    };
  }

  onFileLoaded(formData: any) {
    this.pointeService.postModifiedFileExel(formData)
      .subscribe(events => {
        if (events.type === HttpEventType.UploadProgress) {
        } else if (events.type === HttpEventType.Response) {
         // this.fileUploadProgress = 0;
          console.log(events.body);
          alert('SUCCESS !!');
        }

      });
  }
}

服务

postModifiedFileExel(formData: any) {
    return this.http.post(`${AppUtils.REST_API_SERVER}/espacegroupe/upload-file`, formData ,  {
      reportProgress: true,
      observe: 'events'
    });
  }

我还注意到我的帖子只包含文件的名称,它不包含字节格式的数据

在此处输入图像描述

请问有什么建议吗?

4

1 回答 1

0

对于那些仍然遇到同样问题的人,我将首先解释我如何调试以找出解决方案,然后我做错了什么。

调试

当我处理堆栈跟踪时,我发现了两种负责我收到的消息的方法,即Current request is not a multipart request

public class ServletFileUpload extends FileUpload {
        private static final String POST_METHOD = "POST";

        public static final boolean isMultipartContent(HttpServletRequest request) {
            return !"POST".equalsIgnoreCase(request.getMethod()) ? false : FileUploadBase.isMultipartContent(new ServletRequestContext(request));
        }
      .....
}

    public abstract class FileUploadBase {

        public FileUploadBase() {
        }

        public static final boolean isMultipartContent(RequestContext ctx) {
            String contentType = ctx.getContentType();
            if (contentType == null) {
                return false;
            } else {
                return contentType.toLowerCase(Locale.ENGLISH).startsWith("multipart/");
            }
        }
     ....
}

这意味着如果您的呼叫不同POST或您的内容类型不是以"multipart/"您的播放负载开始的,则被视为非multipart

我注意到我的内容类型总是Content-Type: application/json

但是为什么我总是认为Content-Type: application/jsonformData 应该是Content-Type: multipart/form-data

在我的情况下,这是因为在应用程序中有一个始终设置在 header 中的 url 拦截器Content-Type: application/json

const authReq = req.clone({setHeaders: {'Content-Type': 'application/json'}, withCredentials: true});

为确保您发送正确的数据类型,请确保在您拥有的请求标头中 Content-Type: multipart/form-data;boundary=...并且您的 formData 是二进制的。

在此处输入图像描述

这里有一个链接到帮助我解决这个问题的文章

于 2019-12-27T18:50:28.753 回答