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

Viewed 1352

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..
}
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);
}
Related