Can someone help me? I have a issue in validation of file size limit in Rest controller using multipart but it does not work. I already get the size of multipart and add some if statement to message the exceed limit of the file but its not working.
@RequestMapping(value = "/importCsvFile", method = RequestMethod.POST)
public ResponseEntity<?> importCsvFile(MultipartFile inputFile) throws Exception {
if (inputFile == null || inputFile.isEmpty()) {
return new ResponseEntity<>("Please insert a CSV file.", HttpStatus.NOT_FOUND);
}
if (!inputFile.getContentType().equals("application/vnd.ms-excel")) {
return new ResponseEntity<>("Upload CSV file only.", HttpStatus.NOT_ACCEPTABLE);
}
if (inputFile.getSize() > 1048576) { **====> File size limit**
return new ResponseEntity<>("File too Large.", HttpStatus.NOT_ACCEPTABLE);
}
else {
List<Object> items = objectSrvc.importCsvFile(inputFile.getInputStream());
if (items == null) {
return new ResponseEntity<List<Object>>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<List<Object>>(items, HttpStatus.OK);
}
}