[英]getting error on springboot while upload list of multipartfile
I am trying to upload multiple files and I am facing an issue while doing so.我正在尝试上传多个文件,但在上传时遇到了问题。 Can someone please suggest what is wrong?
有人可以建议有什么问题吗?
I am enclosing relevant snippets of my code to debug better.我附上了我的代码的相关片段以便更好地调试。
html code html代码
<label>
welcome {{name}}, welcome to new app.
</label>
<div>
<input type="file" multiple placeholder="Select Files to be upload" accept=".xlsx" (change)=selectedfiles($event)>
</div>
upload logic上传逻辑
selectedfiles(event){
this.selectedxlfiles=event.target.files;
this.fileandinstancekeyobj.filetoupload=this.selectedxlfiles;
this.fileandinstancekeyobj.instancekey=this.instancekey;
this.uploadservice.uploadtoserver(this.fileandinstancekeyobj).subscribe(result=>{
console.log(result);
})
}
uploadservice上传服务
uploadtoserver(selectedfileandinstacekeyobj): Observable<HttpEvent<{}>>{
let url:string=environment.url+'uploadfile';
const newrequest=new HttpRequest('POST',url,selectedfileandinstacekeyobj,{
reportProgress:true,
responseType:'text'
});
return this.http.request(newrequest);
}
springboot controller弹簧启动 controller
@RestController
public class uploadcontroller {
@PostMapping("/uploadfile")
public ResponseEntity<String> handleupload(@RequestBody uploaddto dto){
System.out.println("sucessfull");
System.out.println(dto.getInstancekey()+" "+dto.getFiletoupload().length);
return ResponseEntity.status(HttpStatus.OK).body("ok");
}
upload DTO上传DTO
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;
class uploaddto {
List<MultipartFile> filetoupload;
String instancekey;
public uploaddto(List<MultipartFile> filetoupload, String instancekey) {
super();
filetoupload=new ArrayList<MultipartFile>();
this.filetoupload = filetoupload;
this.instancekey = instancekey;
}
public List<MultipartFile> getFiletoupload() {
return filetoupload;
}
public void setFiletoupload(List<MultipartFile> filetoupload) {
this.filetoupload = filetoupload;
}
public String getInstancekey() {
return instancekey;
}
public void setInstancekey(String instancekey) {
this.instancekey = instancekey;
}
}
I am receiving the following error我收到以下错误
[org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error:
Cannot deserialize instance of `java.util.ArrayList<org.springframework.web.multipart.MultipartFile>` out of START_OBJECT token;
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException:
Cannot deserialize instance of `java.util.ArrayList<org.springframework.web.multipart.MultipartFile>` out of START_OBJECT token
at [Source: (PushbackInputStream); line: 1, column: 17] (through reference chain: com.example.demo.uploaddto["filetoupload"])]
Any suggestion would be appreciated任何建议将不胜感激
I am adding this answer so that i could help someone save his day, what i did我正在添加这个答案,以便我可以帮助某人挽救他的一天,我所做的
change in uploadcontroller上传控制器的变化
this.selectedxlfiles=event.target.files;
const data:FormData=new FormData();
for(let i=0;i<this.selectedxlfiles.length;i++){
this.currentfile=this.selectedxlfiles[i];
data.append('selectedfile',this.currentfile);
}
data.append('instancekey',this.instancekey);
this.uploadservice.uploadtoserver(data).subscribe(Response=>{
console.log(Response);
})
changes in upload service上传服务的变化
uploadtoserver(data:FormData): Observable<HttpEvent<{}>>{
let url:string=environment.url+'uploadfile';
// console.log(url);
// const data: FormData=new FormData();
// data.append('selectedfile',selectedfile);
// data.append('instancekey',instancekey);
const newrequest=new HttpRequest('POST',url,data,{
reportProgress: true,
responseType: 'text',
});
return this.http.request(newrequest);
//return this.http.post(url,selectedfiles);
}
changes in springboot controller springboot controller 的变化
@RestController
public class uploadcontroller {
@PostMapping("/uploadfile")
public ResponseEntity<String> handleupload(@ModelAttribute uploaddto dto){
System.out.println("sucessfull");
System.out.println(dto.getInstancekey()+" "+dto.getFiletoupload().length);
return ResponseEntity.status(HttpStatus.OK).body("ok");
}
the only change in controller @modelattribute controller @modelattribute 中唯一的变化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.