简体   繁体   English

上传多部分文件列表时在 springboot 上出错

[英]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.

相关问题 尝试使用 springboot 应用程序上传 5gb 文件时出现 memory 错误 - While trying to upload 5gb file using springboot application im getting out of memory error 运行springboot应用程序时出现错误 - Getting error while running springboot application 在 springboot 中设置值时出错? - Getting Error while setting value in springboot? 在springboot中使用@Autowired时出错 - Getting error while using @Autowired in the springboot 在 Springboot REST API 中的 json 中发送外键时出错? - Getting error while sending foreign key in json in Springboot REST API? 在 springboot 中借助 sweetalert2 显示消息时出错 - Getting error while showing message with help of sweetalert2 in springboot 尝试在现有 gradle 项目上启动 springboot 应用程序时出错 - Getting error while trying to start springboot application on existing gradle project 在 SpringBoot 中创建具有多个关系的实体类时出错 - Getting error while creating Entity classes with multiple relationships in SpringBoot 从Feign Client上传MultipartFile并给出403禁止错误 - MultipartFile upload from Feign Client giving 403 Forbidden error 尝试上传文件时,所需的MultipartFile参数&#39;file&#39;不存在错误 - Required MultipartFile parameter 'file' is not present error when trying upload a file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM