0

Hello i need to open excel file with spring boot and add data in database , but i get this error : The constructor File(InputStream) is undefined

the controller :

@PostMapping("/upload")
public ResponseEntity<?> addRfp  (@RequestParam("file") MultipartFile file) throws IOException, InvalidFormatException  {


    OPCPackage pkg = OPCPackage.open(new File(file.getInputStream()));
    XSSFWorkbook wb = new XSSFWorkbook(pkg);
System.out.print(wb.getAllNames()); 


    return null;

}

the front end partie :

state = {
  file : null
}
handlFile(e){

  let file = e.target.files[0]
  this.setState({file : file})
}

handleUpload(e){

   let file  = this.state.file;
   let formdata  = new FormData()
   formdata.append('file', file)
   formdata.append("name","ELMANDOUR AMINE")
 axios({
  url :'http://localhost:8080/rfp/upload',
  method :'POST',

  data : formdata
 }).then((res)=>{})
}

the form :

input type="file" className="form-control" id="file" name="file"  onChange={(e)=>this.handlFile(e)} required/>
       </div>
       </div>
       <button id="button" type="button" onClick={(e)=>this.handleUpload(e)}>    <img src={add} id="add" alt="Ajouter Region" />  </button>

please what i should to do i need to open the file excel and add the data at my database but when i try to open the file i get error what i should to do please !

Amine El Mandour
  • 85
  • 1
  • 4
  • 13

1 Answers1

0

You are getting the constructor undefined error because java File class does not have a constructor which expects streams. See File class constructors

Also when you are getting a file directly using multipart then why you need to load it again?

For opening a file using OPCPackage refer this thread

Alien
  • 15,141
  • 6
  • 37
  • 57