简体   繁体   中英

How can I receive multipart/form-data in Spring MVC Controller?

I need to implement a REST-Endpoint, that receives multipart/form-data
I use:

  • Spring Boot
  • Kotlin
  • Spring MVC

A multipart form submit with the following parts:

deployment-name ----- text/plain
enable-duplicate-filtering ----- text/plain
deploy-changed-only ----- text/plain
deployment-source ----- text/plain
tenant-id ----- text/plain
* ----- application/octet-stream

The Rest Controller looks so:

    @PostMapping("/data/deployment/create")
    fun uploadDmn(
            @RequestParam("deployment-name")
            deploymentName: String,
            @RequestParam("enable-duplicate-filtering")
            enableDuplicateFiltering: String?,
            @RequestParam("deploy-changed-only")
            deployChangedOnly: String,
            @RequestParam("deployment-source")
            deploymentSource: String,
            @RequestParam("tenant-id")
            tenantId: String,
            @RequestParam("data")
            data: MultipartFile
    ) {
        println(deploymentName)
        println(deployChangedOnly)
        println(deploymentSource)
        println(tenantId)
        println(data.toString())
    }

For all params that works, but for the last one that doesn't work. I've tried to give a name "data", "*", "file" that doesn't work.

Required request part 'data' is not present

The Controller doesn't see that file.

I've tried too to use Retrofit:

    @PostMapping("/data/deployment/create")
    @Multipart
    fun uploadDmn(
            @Part("data")
            data: MultipartFile
    ) {
        println(data.toString())
}

But that doesn't work too:

Parameter specified as non-null is null

How can I work with that content type? multipart/form-data

Example of Request:

--28319d96a8c54b529aa9159ad75edef9
Content-Disposition: form-data; name="deployment-name"

aName
--28319d96a8c54b529aa9159ad75edef9
Content-Disposition: form-data; name="enable-duplicate-filtering"

true
--28319d96a8c54b529aa9159ad75edef9
Content-Disposition: form-data; name="deployment-source"

process application
--28319d96a8c54b529aa9159ad75edef9
Content-Disposition: form-data; name="data"; filename="test.bpmn"

<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions ...>
  <!-- BPMN 2.0 XML omitted -->
</bpmn2:definitions>
--28319d96a8c54b529aa9159ad75edef9--

Can anyone help please?

I hope I can help you with that. Try it.

    @RequestMapping(value = "/putRequest",
        produces = { "application/json" }, 
        consumes = { "multipart/form-data" },
        method = RequestMethod.PUT)
    public ResponseEntity<SuccessDto> requestPut(@Valid @RequestParam(value = "commit", required = false, defaultValue="false") Boolean commit, @Valid @RequestPart("file") MultipartFile file) {
        return new ResponseEntity<>(HttpStatus.OK);
    }

You can use something like this I guess:

@RequestMapping(value = "/source/upload/{sourceName}", method = RequestMethod.POST)
    public List<String> uploadSource(@RequestParam("file") MultipartFile sourceFile,
            @PathVariable("sourceName") String sourceName) throws IOException {
        File outputFile = new File(System.getProperty("java.io.tmpdir"), sourceFile.getOriginalFilename());
        sourceFile.transferTo(outputFile);
        <<your business logic>>
        Files.deleteIfExists(outputFile.toPath());
        return Arrays.asList(columns);

    }

Note that I have created a copy of the file in the temp-directory (via code) and am using the file there to implement my business logic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM