天天看點

上傳檔案遇到the request was rejected because no multipart boundary was found

上傳檔案遇到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

上傳檔案遇到the request was rejected because no multipart boundary was found

可以試試 把接口中的 headers: {‘Content-Type’: ‘multipart/form-data’} 登出掉

然後使用 FormData 上傳

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

然後你看, 也有了

上傳檔案遇到the request was rejected because no multipart boundary was found