努力通过REST

时间:2019-04-04 09:02:22

标签: java rest spring-boot

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

当我触发REST调用时,我在REST Client中收到以下错误消息:

{
  "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]

2 个答案:

答案 0 :(得分:0)

由于没有在应用程序中指定多格式数据的限制,所以您遇到此异常。因此,必须指定该限制。

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

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

享受编码。

答案 1 :(得分:0)

我使用了另一种方法,而不是使用MultipartFile。现在,我将简单字节数组用作REST接口的参数。见下文:

    Connection hikariCon = statement.getConnection();
    if (hikariCon.isWrapperFor(OracleConnection.class)) {
        oracleConection = hikariCon.unwrap(OracleConnection.class);
    }
    final STRUCT struct = new STRUCT(StructDescriptor.createDescriptor(DB_OBJECT_TYPE, oracleConection),
            oracleConection, values);
    statement.setObject(index, struct, SQL_TYPE);