春季启动 React js 上传文件

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

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(&quot;/upload&quot;)
public ResponseEntity&lt;?&gt; addRfp  (@RequestParam(&quot;file&quot;) 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(&#39;file&#39;, file)
   formdata.append(&quot;name&quot;,&quot;ELMANDOUR AMINE&quot;)
 axios({
  url :&#39;http://localhost:8080/rfp/upload&#39;,
  method :&#39;POST&#39;,
    
  data : formdata
 }).then((res)=&gt;{})
}

the form :

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;
       &lt;/div&gt;
       &lt;/div&gt;
       &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:

确定