Javascript 将csv文件传递到SpringBoot java服务器
我的reactjs前端有一个文件类型的输入字段。我想上传一个CSV文件并将其发送到使用SpringBoot的服务器。我有问题,如何获得该文件一旦被提取到我的后端 这是我在渲染方法中的文件输入:Javascript 将csv文件传递到SpringBoot java服务器,javascript,java,reactjs,csv,spring-boot,Javascript,Java,Reactjs,Csv,Spring Boot,我的reactjs前端有一个文件类型的输入字段。我想上传一个CSV文件并将其发送到使用SpringBoot的服务器。我有问题,如何获得该文件一旦被提取到我的后端 这是我在渲染方法中的文件输入: <input accept=".csv" type="file" onChange={this.handleChange} /> <Button primary onClick={this.handleSubmit}> Submit </Button&
<input
accept=".csv"
type="file"
onChange={this.handleChange}
/>
<Button primary onClick={this.handleSubmit}> Submit </Button>
我的后端控制器是
@PostMapping(INGEST_URL)
public String ingestDataFile(@RequestBody ??? response) {
return "temporary return";
}
ingestDataFile()
应该采用什么作为请求主体,也就是说,打印文件的内容。您的端点应该大致如下所示
@PostMapping(INGEST_URL)
public String ingestDataFile(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
if (file.isEmpty()) {
redirectAttributes.addFlashAttribute("message", "No File is Present");
return "redirect:uploadStatus";
}
try {
// Get the file and save it somewhere
byte[] bytes = file.getBytes();
Path path = Paths.get("FOLDER TO UPLOAD TO" + file.getOriginalFilename());
Files.write(path, bytes);
redirectAttributes.addFlashAttribute("message",
"File upload successful'" + file.getOriginalFilename() + "'");
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/uploadStatus";
}
@GetMapping("/uploadStatus")
public String uploadStatus() {
return "uploadStatus";
}
您的端点应该大致如下所示
@PostMapping(INGEST_URL)
public String ingestDataFile(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
if (file.isEmpty()) {
redirectAttributes.addFlashAttribute("message", "No File is Present");
return "redirect:uploadStatus";
}
try {
// Get the file and save it somewhere
byte[] bytes = file.getBytes();
Path path = Paths.get("FOLDER TO UPLOAD TO" + file.getOriginalFilename());
Files.write(path, bytes);
redirectAttributes.addFlashAttribute("message",
"File upload successful'" + file.getOriginalFilename() + "'");
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/uploadStatus";
}
@GetMapping("/uploadStatus")
public String uploadStatus() {
return "uploadStatus";
}
参数应为:
@PostMapping(INGEST_URL)
public String ingestDataFile(@RequestParam("file") MultipartFile file) {
System.out.println("Name is::: " + file.getName());
// Get file in byte[] using file.getBytes()
storeCSVInDbServer(file);
return "temporary return";
}
如果要将文件存储在数据库服务器中,可以将其存储在数据库的BLOB列中。参数应为:
@PostMapping(INGEST_URL)
public String ingestDataFile(@RequestParam("file") MultipartFile file) {
System.out.println("Name is::: " + file.getName());
// Get file in byte[] using file.getBytes()
storeCSVInDbServer(file);
return "temporary return";
}
如果要将文件存储在数据库服务器中,可以将其存储在数据库的BLOB列中