Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone help me on consuming text file in rest-api?

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..
}
like image 249
Srija Myaka Avatar asked Nov 19 '25 07:11

Srija Myaka


1 Answers

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);
}
like image 83
Akshay Mishra Avatar answered Nov 21 '25 21:11

Akshay Mishra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!