簡體   English   中英

上傳多部分文件列表時在 springboot 上出錯

[英]getting error on springboot while upload list of multipartfile

我正在嘗試上傳多個文件,但在上傳時遇到了問題。 有人可以建議有什么問題嗎?

我附上了我的代碼的相關片段以便更好地調試。

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>

上傳邏輯

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);
    })

  }

上傳服務

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);
  }

彈簧啟動 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");
    }

上傳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;
    }
    
    
}

我收到以下錯誤

[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"])]

任何建議將不勝感激

我正在添加這個答案,以便我可以幫助某人挽救他的一天,我所做的

上傳控制器的變化

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);
})

上傳服務的變化

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);
  }

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");
    }

controller @modelattribute 中唯一的變化

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM