请求被拒绝,因为未找到多部分边界。

huangapple go评论90阅读模式
英文:

the request was rejected because no multipart boundary was found java

问题

我正在开发文件上传功能。为此,我有一个Spring Boot Web服务,客户端是Angular。

Web服务:-

@PostMapping(value = "/sendmails", headers = "content-type=multipart/*", consumes = "application/*")
public ResponseEntity<String> sendEmails(@RequestParam("data") MultipartFile reapExcelDataFile) {

    return new ResponseEntity("Success", HttpStatus.OK);
}

UI:-

postFile(fileToUpload: File): Observable<boolean> {
  const endpoint = 'http://localhost:8080/sendmails';
  debugger;
  let config =  {headers: new  HttpHeaders({ 'Content-Type': 'multipart/form-data'})};
  const formData: FormData = new FormData();
  formData.append('data', fileToUpload);
  return this.httpClient.post(endpoint, formData, config)
    .pipe(map(() => { return true; }));
}

错误:-

> 2020-07-31 16:13:19.644 ERROR 13144 --- [nio-8080-exec-2]
> o.a.c.c.C.[.[.[/].[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.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:834)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:256)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:280)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.catalina.connector.Request.parseParts(Request.java:2881)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.catalina.connector.Request.parseParameters(Request.java:3214)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.catalina.connector.Request.getParameter(Request.java:1116)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:84)
> ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE] 	at
> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
> ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE] 	at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
> ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE] 	at
> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
> ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE] 	at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
> [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
> [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
> [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
> [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
> [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
> [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
> [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
> [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
> [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
> [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
> org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
>
<details>
<summary>英文:</summary>
I am developing a file upload functionality. for that I have a spring boot web service and client is angular.
**Webservice:-**
@PostMapping(value = &quot;/sendmails&quot;, headers = &quot;content-type=multipart/*&quot;, consumes = &quot;application/*&quot;)
public ResponseEntity&lt;String&gt; sendEmails(@RequestParam(&quot;data&quot;) MultipartFile reapExcelDataFile) {
return new ResponseEntity(&quot;Success&quot;, HttpStatus.OK);
}
**UI: -**
postFile(fileToUpload: File): Observable&lt;boolean&gt; {
const endpoint = &#39;http://localhost:8080/sendmails&#39;;
debugger;
let config =  {headers: new  HttpHeaders({ &#39;Content-Type&#39;: &#39;multipart/form-data&#39;})};
const formData: FormData = new FormData();
formData.append(&#39;data&#39;, fileToUpload);
return this.httpClient.post(endpoint, formData, config)
.pipe(map(() =&gt; { return true; }));
}
**ERROR: -**
&gt; 2020-07-31 16:13:19.644 ERROR 13144 --- [nio-8080-exec-2]
&gt; o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for
&gt; servlet [dispatcherServlet] in context with path [] threw exception
&gt; [Request processing failed; nested exception is
&gt; org.springframework.web.multipart.MultipartException: Failed to parse
&gt; multipart servlet request; nested exception is java.io.IOException:
&gt; org.apache.tomcat.util.http.fileupload.FileUploadException: the
&gt; request was rejected because no multipart boundary was found] with
&gt; root cause
&gt; 
&gt; org.apache.tomcat.util.http.fileupload.FileUploadException: the
&gt; request was rejected because no multipart boundary was found 	at
&gt; org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.&lt;init&gt;(FileUploadBase.java:834)
&gt; ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:256)
&gt; ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:280)
&gt; ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.catalina.connector.Request.parseParts(Request.java:2881)
&gt; ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.catalina.connector.Request.parseParameters(Request.java:3214)
&gt; ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.catalina.connector.Request.getParameter(Request.java:1116)
&gt; ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381)
&gt; ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:84)
&gt; ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE] 	at
&gt; org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
&gt; ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE] 	at
&gt; org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
&gt; ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
&gt; ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
&gt; ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE] 	at
&gt; org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
&gt; ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE] 	at
&gt; org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
&gt; ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
&gt; ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
&gt; ~[tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
&gt; [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
&gt; [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
&gt; [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
&gt; [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
&gt; [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
&gt; [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
&gt; [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
&gt; [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
&gt; [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
&gt; [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
&gt; [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
&gt; [na:1.8.0_251] 	at
&gt; java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
&gt; [na:1.8.0_251] 	at
&gt; org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
&gt; [tomcat-embed-core-9.0.21.jar:9.0.21] 	at
&gt; java.lang.Thread.run(Thread.java:748) [na:1.8.0_251]
If I remove the file and give a string then the call is going. What might be the issue.
</details>
# 答案1
**得分**: 0
问题在于我自己设置了`Content-Type`,让它保持空白。Google Chrome会为我完成这个操作。多部分`Content-Type`需要知道文件边界,当你移除`Content-Type`时,Postman会自动为你完成。
<details>
<summary>英文:</summary>
The problem is that I am setting the `Content-Type` by myself, let it be blank. Google Chrome will do it for me. The multipart `Content-Type` needs to know the file boundary, and when you remove the `Content-Type`, Postman will do it automagically for you.
</details>
# 答案2
**得分**: 0
If you are using angular 15+ with restful spring api, I did encapsulate the file in ***FormData*** object and it worked : 
const formData = new FormData();
formData.append("file", file, file.name);
return this.http.post<any>(`${env.apiUrl}/controllerUrl/uploadFile`, formData);
<details>
<summary>英文:</summary>
If you are using angular 15+ with restful spring api, I did encapsulated the file in ***FormData*** object and it worked : 
const formData = new FormData();
formData.append(&quot;file&quot;, file, file.name);
return this.http.post&lt;any&gt;(`${env.apiUrl}/controllerUrl/uploadFile`, formData);
</details>

huangapple
  • 本文由 发表于 2020年7月31日 18:42:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63190354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定