I have below code which consumes input using @RequestBody. But I'm trying to write a @PostMapping request which consumes a text file. But I'm not aware on how to achieve my desired requirement. Can someone please help me on achieving the desired result?
@PostMapping(value = "/getInput")
public ResponseEntity<ApiResponse> getInput(@RequestBody InputData inputData) {
// rest of the code..
}
You can try something as below:
@PostMapping(value = "/getInput")
public ResponseEntity<String> uploadFile(@RequestPart("file") MultipartFile file) {
try {
byte[] bytes = file.getBytes();
Path path = Paths.get(file.getOriginalFilename());
Files.write(path, bytes);
System.out.println(path.getFileName());
} catch (IOException e) {
System.out.println(e.getMessage());
}
return new ResponseEntity<>("Good Job", HttpStatus.OK);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With