Java 上载多部分文件列表时获取springboot错误
我试图上传多个文件,但在上传过程中遇到了一个问题。有人能告诉我出了什么问题吗 为了更好地进行调试,我附上了相关的代码片段 html代码Java 上载多部分文件列表时获取springboot错误,java,typescript,spring-boot,angular8,Java,Typescript,Spring Boot,Angular8,我试图上传多个文件,但在上传过程中遇到了一个问题。有人能告诉我出了什么问题吗 为了更好地进行调试,我附上了相关的代码片段 html代码 <label> welcome {{name}}, welcome to new app. </label> <div> <input type="file" multiple placeholder="Select Files to be upload" acce
<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>
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);
}
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);
}
上传服务
<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>
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);
}
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);
}
uploadtoserver(selectedfileandinstacekeyobj):可观察{
让url:string=environment.url+'uploadfile';
const newrequest=newhttprequest('POST',url,selectedfileandinstacekeyobj{
报告进展:是的,
响应类型:'text'
});
返回此.http.request(newrequest);
}
springboot控制器
@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");
}
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);
})
@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");
}
@RestController
公共类上载控制器{
@PostMapping(“/uploadfile”)
公共响应handleupload(@RequestBody上传到dto){
System.out.println(“successfull”);
System.out.println(dto.getInstancekey()+“”+dto.getFiletoupload().length);
返回ResponseEntity.status(HttpStatus.OK).body(“OK”);
}
上传数据到
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;
}
}
import java.util.ArrayList;
导入java.util.LinkedList;
导入java.util.List;
导入org.springframework.web.multipart.MultipartFile;
类上载到{
列表文件上传;
字符串实例键;
公共上传到(列表文件上传,字符串实例键){
超级();
filetoupload=newarraylist();
this.filetoupload=filetoupload;
this.instancekey=instancekey;
}
公共列表getFiletoupload(){
返回filetoupload;
}
public void setFiletoupload(列表filetoupload){
this.filetoupload=filetoupload;
}
公共字符串getInstancekey(){
返回instancekey;
}
public void setInstancekey(字符串instancekey){
this.instancekey=instancekey;
}
}
我收到以下错误
[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"])]
[org.springframework.http.converter.httpMessageNodeTableException:JSON解析错误:
无法反序列化'java.util.ArrayList'的实例,该实例不在起始\u对象标记中;
嵌套异常为com.fasterxml.jackson.databind.exc.MismatchedInputException:
无法反序列化“java.util.ArrayList”的实例,该实例不在起始\u对象标记中
在[源:(PushbackInputStream);行:1,列:17](通过引用链:com.example.demo.uploaddto[“filetoupload”])]
如果您有任何建议,我们将不胜感激我将添加此答案,以便帮助他人挽救我所做的一切 上传控制器的更改
@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");
}
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);
})
@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");
}
this.selectedxlfiles=event.target.files;
const data:FormData=new FormData();
for(设i=0;i{
控制台日志(响应);
})
上传服务的更改
<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>
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);
}
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);
}
uploadtoserver(数据:FormData):可观察{
让url:string=environment.url+'uploadfile';
//console.log(url);
//const data:FormData=new FormData();
//data.append('selectedfile',selectedfile);
//data.append('instancekey',instancekey);
const newrequest=newhttprequest('POST',url,data{
报告进展:是的,
responseType:'文本',
});
返回此.http.request(newrequest);
//返回此.http.post(url,selectedfiles);
}
springboot控制器的更改
@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");
}
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);
})
@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");
}
@RestController
公共类上载控制器{
@PostMapping(“/uploadfile”)
公共响应handleupload(@ModelAttribute上载到dto){
System.out.println(“successfull”);
System.out.println(dto.getInstancekey()+“”+dto.getFiletoupload().length);
返回ResponseEntity.status(HttpStatus.OK).body(“OK”);
}
controller@modelattribute中唯一的更改是“我收到的错误已解决”,这意味着什么?你自己解决了问题吗?@Vishal在控制台中的错误消息出现之前,这里没有解决任何问题。你从JS端发送多部分数据的方式是错误的。你必须使用formdata。@ImeshaSudasingha hi在form data中追加了数据。我现在收到的错误是[org.springframework.web.HttpMediaTypeNotSupportedException:Content type'multipart/form data;boundary=----WebKitFormBoundarynSmJd8a4udBVYO1k;charset=UTF-8'notsupported]您是否尝试过指定控制器端点/方法接受的内容?我认为默认情况下它接受JSON。要在Spring Boot中做到这一点,您需要使用“consumes”属性。例如@PostMapping(path=“/uploadfile”,consumes=“multipart/form data”)