英文:
Spring boot React js Upload file
问题
你好,我需要使用Spring Boot打开Excel文件并将数据添加到数据库中,
但是我遇到了这个错误:构造函数File(InputStream)未定义
控制器部分:
@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;
}
前端部分:
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) => {})
}
表单部分:
<input type="file" className="form-control" id="file" name="file" onChange={(e) => this.handlFile(e)} required/>
<button id="button" type="button" onClick={(e) => this.handleUpload(e)}> <img src={add} id="add" alt="Ajouter Region" /> </button>
请问我应该怎么做,我需要打开Excel文件并将数据添加到数据库中,但是当我尝试打开文件时出现错误,我应该怎么做呢?
英文:
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 !
答案1
得分: 0
因为Java的File
类没有接受流的构造函数,所以你会遇到构造函数未定义的错误。请参阅File类构造函数。
另外,如果你已经直接使用多部分获取了文件,为什么还需要再次加载它呢?
如果想要使用OPCPackage打开文件,请参考此讨论串。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论