gpt4 book ai didi

java - 难以通过 REST 上传文件

转载 作者:行者123 更新时间:2023-12-02 10:02:42 24 4
gpt4 key购买 nike

如果在 SpringBoot 应用程序中通过 REST 上传文件时遇到一些困难。为了测试 REST 接口(interface),我使用 Insomnia 或有时使用 Curl。在 Insomnia 中,我已配置为使用调用作为 POST。此外,我附加了一个简单的 .txt 文件,其中包含名称和一些仅“测试”的内容。我使用 Content-Type = multipart/form-data 作为 header 。

当我触发 REST 调用时,我在 REST 客户端内收到以下错误消息:

{
"timestamp": "2019-04-04T08:51:19.743+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Failed to parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found",
"path": "/upload/upload_file"
}

我的 RestController 看起来像这样:

@RestController
@RequestMapping("/upload")
public class FileUploadController {

...

@PostMapping("/upload_file")
public String upload(@RequestParam("file") MultipartFile file) {
return "test";
}

发送 REST 调用后,服务器显示以下错误消息:

org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:990) ~[commons-fileupload-1.3.jar:1.3]
at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:310) ~[commons-fileupload-1.3.jar:1.3]
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:334) ~[commons-fileupload-1.3.jar:1.3]
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:115) ~[commons-fileupload-1.3.jar:1.3]
at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:158) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:142) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1175) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1010) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]

最佳答案

您收到此异常是因为您尚未在应用程序中指定多形式数据的限制。因此,您必须指定该限制。

只需在“application.properties”文件或“application.yml”文件中添加以下行即可。

spring.http.multipart.max-file-size=20MB

享受编码的乐趣..

关于java - 难以通过 REST 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55511880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com