如何使用Spring Boot上传多个文件并使用cURL对其进行测试 [英] Howto upload multiple files with Spring Boot and test it with cURL
本文介绍了如何使用Spring Boot上传多个文件并使用cURL对其进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经实现了一个控制器来上传多个文件:
I have implemented a Controller to upload multiple files:
public class Image implements Serializable {
private MultipartFile file;
private Ingeger imageNumber;
...
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void handleFileUpload(@RequestBody Set<Image> images) {
...
}
我正确地使用使用此命令直接在上传方法中仅一个 MultipartFile:
I correctly checked the code using only one MultipartFile directly in the upload method using this command:
curl http://localhost:8080/upload -X POST -F 'file=@1.jpg;type=image/jpg' -H "Content-Type: multipart/form-data"
我需要以三种方式扩展它,但不知道正确的语法:
I need to extend it in three ways but don't know the correct syntax:
- 发布JSON项目的集合
- 为每个项目添加字段 imageNumber
- 最棘手的部分:为每个项目添加嵌套的文件item
推荐答案
我使用数组而不是带有嵌套文件的Set来解决它。
I solved it using an Array instead of a Set with nested files.
p>
Java:
Java:
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void handleFileUpload(@RequestParam MultipartFile[] images, @RequestParam Integer[] numbers) {
...
}
cURL:
curl http://localhost:8080/upload -X POST \
-F 'files=@1.jpg;type=image/jpg' \
-F 'numbers=1' \
-F 'files=@2.jpg;type=image/jpg' \
-F 'numbers=2' \
-F 'files=@3.jpg;type=image/jpg' \
-F 'numbers=3'
这篇关于如何使用Spring Boot上传多个文件并使用cURL对其进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文