Java 空列表<;多部分文件>;在Spring中尝试使用ng file upload上载多个文件时
我有以下控制器方法可以一次上传多个文件,其灵感来源和答案如下: 我可以看到它正确地包含我上传的文件: 空Java 空列表<;多部分文件>;在Spring中尝试使用ng file upload上载多个文件时,java,angularjs,spring,spring-mvc,ng-file-upload,Java,Angularjs,Spring,Spring Mvc,Ng File Upload,我有以下控制器方法可以一次上传多个文件,其灵感来源和答案如下: 我可以看到它正确地包含我上传的文件: 空列表的原因可能是什么 我正在使用提交文件,但我认为这与问题无关。Spring 4.2.4.试着像这样使用@ModelAttribute: @RequestMapping(value = "/{user}/attachment", method = RequestMethod.POST) @PreAuthorize(...) public void upload(@P
列表的原因可能是什么
我正在使用提交文件,但我认为这与问题无关。Spring 4.2.4.试着像这样使用@ModelAttribute
:
@RequestMapping(value = "/{user}/attachment", method = RequestMethod.POST)
@PreAuthorize(...)
public void upload(@PathVariable User user,@ModelAttribute("uploadFile") FileUpload uploadFile) throws IllegalStateException, IOException {
List<MultipartFile> files = uploadFile.getFiles();
...
@RequestMapping(value=“/{user}/attachment”,method=RequestMethod.POST)
@预授权(…)
public void upload(@PathVariable User User,@modeldattribute(“uploadFile”)FileUpload uploadFile)引发IllegalStateException,IOException{
List files=uploadFile.getFiles();
...
并创建一个类,如:
public class FileUpload {
private List<MultipartFile> files;
public List<MultipartFile> getFiles() {
return files;
}
public void setFiles(List<MultipartFile> files) {
this.files= files;
}
}
公共类文件上传{
私人清单文件;
公共列表getFiles(){
归还文件;
}
公共无效设置文件(列出文件){
this.files=文件;
}
}
我认为,按照您从前端发送数据的方式,它不能绑定到java.util.List。如果您创建一个JSON数据作为请求,并使用@RequestBody对列表进行注释,如:
@RequestMapping(value = "/{user}/attachment", method = RequestMethod.POST)
@PreAuthorize(...)
public void upload(@PathVariable User user,
@RequestBody List<MultipartFile> files) {
// handle files
}
@RequestMapping(value=“/{user}/attachment”,method=RequestMethod.POST)
@预授权(…)
公共无效上载(@PathVariable用户,
@请求主体列表文件){
//处理文件
}
这应该行得通。一些信息。问题是默认情况下,使用名称文件[0]
,文件[1]提交文件数组
等。在使用上传
服务时,可使用该值进行配置。将其设置为空字符串将强制使用相同的文件
键发送文件,该键通过Spring正确解析,@RequestParam(“文件”)列表包含已提交的所有文件
Upload.upload({url: url, data: {file: arrayOfFiles}, arrayKey: ''})
这对我来说很有用,将带有多个文件附件的大型“电子邮件”对象从UI发送到后端:
棱角的
sendEmailWithAttachments(taskId: string, template: string, email: any, modelConfig: any, files: any[]) {
let formData = new FormData();
formData.append('form', new Blob([JSON.stringify(email)], {type: 'application/json'}));
files.forEach(file => {
formData.append('files', file);
});
return this.$http({
method: 'POST',
data: formData,
url: this.baseUrl + '/' + taskId + '/email-with-attachment?template=' + template,
headers: {
'Content-Type': undefined
},
responseType: 'arraybuffer'
});
}
爪哇之春
@RequestMapping(value = "{taskId}/email-with-attachment", method = RequestMethod.POST, consumes = MULTIPART_FORM_DATA_VALUE)
public void sendEmailWithAttachment(
@PathVariable String taskId,
@RequestParam String template,
@RequestParam("form") MultipartFile form,
@RequestParam("files") List<MultipartFile> files) throws IOException {
Map<String, String> parameters = new ObjectMapper().readValue(form.getInputStream(), HashMap.class);
System.out.println("taskId"+ taskId);
System.out.println("template"+ template);
System.out.println("files"+ files);
System.out.println("parameters"+ parameters);
}
@RequestMapping(value=“{taskId}/email with attachment”,method=RequestMethod.POST,consumes=MULTIPART\u FORM\u DATA\u value)
带有附件的公共无效发送电子邮件(
@PathVariable字符串taskId,
@RequestParam字符串模板,
@RequestParam(“表单”)多部分文件表单,
@RequestParam(“文件”)列表文件)引发IOException{
映射参数=new ObjectMapper().readValue(form.getInputStream(),HashMap.class);
System.out.println(“taskId”+taskId);
System.out.println(“模板”+模板);
System.out.println(“文件”+文件);
System.out.println(“参数”+参数);
}
用于多个文件。在javascript中执行此操作
//first add files to form data
var formData = new FormData();
for (let i = 0; i < files.length; i++) {
formData.append("images", files[i]);
}
//post files to backend e.g using angular
$http.post('upload', formData, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.then(function(response){
console.log("UPLOAD COMPLETE::=> ", response);
}, function (error) {
console.log(error);
});
但是我想一次上载多个文件。您的配置?您需要一个库和一个匹配的解析器来进行多部分上载。我假设您阅读了您所指的博客文章。commons fileupload
例如,是的,我已经完成了所有设置。事实上,我可以通过从multipartRequest.getFileMap().values()读取这些文件来接收这些文件
。我想问的是,为什么我不能像我提到的源代码中建议的那样简单地使用列表
。可能缺少一些东西。例如,如果包括标准的servlet多部分支持,但您使用commons多部分解析程序(如博客文章中建议的那样)
文件将不会被填充。是否可以获得List with List in request?没有为我设置框,但我更喜欢这种方法。最终使用了上面的@francz方法。事实上,当请求主体直接转换为后端的域对象时,您建议的@RequestBody解决方案是正确的。例如,它可以用于从JSON dat解析主体一个对象。但是,它不能与MultipartFile一起使用。我能得到List和List吗?不能用这种API结构格式,但你可以用这两个列表创建一个POJO并尝试使用它。
@RequestMapping(value = "{taskId}/email-with-attachment", method = RequestMethod.POST, consumes = MULTIPART_FORM_DATA_VALUE)
public void sendEmailWithAttachment(
@PathVariable String taskId,
@RequestParam String template,
@RequestParam("form") MultipartFile form,
@RequestParam("files") List<MultipartFile> files) throws IOException {
Map<String, String> parameters = new ObjectMapper().readValue(form.getInputStream(), HashMap.class);
System.out.println("taskId"+ taskId);
System.out.println("template"+ template);
System.out.println("files"+ files);
System.out.println("parameters"+ parameters);
}
//first add files to form data
var formData = new FormData();
for (let i = 0; i < files.length; i++) {
formData.append("images", files[i]);
}
//post files to backend e.g using angular
$http.post('upload', formData, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.then(function(response){
console.log("UPLOAD COMPLETE::=> ", response);
}, function (error) {
console.log(error);
});
//your java method signature
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE )
public Response uploadImage(RequestParam(value = "images") MultipartFile[] images){
}