上传文件遇到the request was rejected because no multipart boundary was found

在这里插入图片描述
postman 这样测试是ok的,
前端的上传接口是这么写的

     return axios({
    
    
      url: '/upload',
      method: 'post',
      headers: {
    
    'Content-Type':  'multipart/form-data'}
      data: file
    })

没毛病 , 就是后端报错

[http-nio-3141-exec-8] ERROR o.a.c.c.C.[.[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found] with root cause
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
	at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.init(FileItemIteratorImpl.java:174)
	at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.getMultiPartStream(FileItemIteratorImpl.java:190)
	at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.findNextItem(FileItemIteratorImpl.java:209)
	at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.<init>(FileItemIteratorImpl.java:127)
	at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:256)
	at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:280)
	at org.apache.catalina.connector.Request.parseParts(Request.java:2922)
	at org.apache.catalina.connector.Request.getParts(Request.java:2824)

请打开postman的Generate Code 发现 Content-Type 变成了Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW 多了 boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
在这里插入图片描述
可以试试 把接口中的 headers: {‘Content-Type’: ‘multipart/form-data’} 注销掉
然后使用 FormData 上传

  let formData = new FormData()
  formData.append('file', file)

然后你看, 也有了
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/xy3233/article/details/119802219