春季启动 React js 上传文件

huangapple go评论103阅读模式
英文:

Spring boot React js Upload file

问题

  1. 你好,我需要使用Spring Boot打开Excel文件并将数据添加到数据库中,
  2. 但是我遇到了这个错误:构造函数File(InputStream)未定义
  3. 控制器部分:
  4. @PostMapping("/upload")
  5. public ResponseEntity<?> addRfp(@RequestParam("file") MultipartFile file) throws IOException, InvalidFormatException {
  6. OPCPackage pkg = OPCPackage.open(new File(file.getInputStream()));
  7. XSSFWorkbook wb = new XSSFWorkbook(pkg);
  8. System.out.print(wb.getAllNames());
  9. return null;
  10. }
  11. 前端部分:
  12. state = {
  13. file: null
  14. }
  15. handlFile(e) {
  16. let file = e.target.files[0]
  17. this.setState({ file: file })
  18. }
  19. handleUpload(e) {
  20. let file = this.state.file;
  21. let formdata = new FormData()
  22. formdata.append('file', file)
  23. formdata.append("name", "ELMANDOUR AMINE")
  24. axios({
  25. url: 'http://localhost:8080/rfp/upload',
  26. method: 'POST',
  27. data: formdata
  28. }).then((res) => {})
  29. }
  30. 表单部分:
  31. <input type="file" className="form-control" id="file" name="file" onChange={(e) => this.handlFile(e)} required/>
  32. <button id="button" type="button" onClick={(e) => this.handleUpload(e)}> <img src={add} id="add" alt="Ajouter Region" /> </button>
  33. 请问我应该怎么做,我需要打开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 :

  1. @PostMapping(&quot;/upload&quot;)
  2. public ResponseEntity&lt;?&gt; addRfp (@RequestParam(&quot;file&quot;) MultipartFile file) throws IOException, InvalidFormatException {
  3. OPCPackage pkg = OPCPackage.open(new File(file.getInputStream()));
  4. XSSFWorkbook wb = new XSSFWorkbook(pkg);
  5. System.out.print(wb.getAllNames());
  6. return null;
  7. }

the front end partie :

  1. state = {
  2. file : null
  3. }
  4. handlFile(e){
  5. let file = e.target.files[0]
  6. this.setState({file : file})
  7. }
  8. handleUpload(e){
  9. let file = this.state.file;
  10. let formdata = new FormData()
  11. formdata.append(&#39;file&#39;, file)
  12. formdata.append(&quot;name&quot;,&quot;ELMANDOUR AMINE&quot;)
  13. axios({
  14. url :&#39;http://localhost:8080/rfp/upload&#39;,
  15. method :&#39;POST&#39;,
  16. data : formdata
  17. }).then((res)=&gt;{})
  18. }

the form :

  1. input type=&quot;file&quot; className=&quot;form-control&quot; id=&quot;file&quot; name=&quot;file&quot; onChange={(e)=&gt;this.handlFile(e)} required/&gt;
  2. &lt;/div&gt;
  3. &lt;/div&gt;
  4. &lt;button id=&quot;button&quot; type=&quot;button&quot; onClick={(e)=&gt;this.handleUpload(e)}&gt; &lt;img src={add} id=&quot;add&quot; alt=&quot;Ajouter Region&quot; /&gt; &lt;/button&gt;

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

huangapple
  • 本文由 发表于 2020年4月7日 03:20:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/61067371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定