英文:
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 = "/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; }));
}
**ERROR: -**
> 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)
> [tomcat-embed-core-9.0.21.jar:9.0.21] at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
> [na:1.8.0_251] at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
> [na:1.8.0_251] at
> org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
> [tomcat-embed-core-9.0.21.jar:9.0.21] at
> 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("file", file, file.name);
return this.http.post<any>(`${env.apiUrl}/controllerUrl/uploadFile`, formData);
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论