'@RequestParam and MultipartFile logic for Optimisation
Scenario: I have a spring boot endpoint, where few XML files are submitted and some operations take place. To improve the performance, it is discussed to not upload the files everytime(submission) , but rather store in the platform(during the 1st submission). And then, the next time, the user does not need to submit the files, but retreive from the cache. However, I need suggestions below:
@PostMapping(path = "/endpoint")
public ResponseEntity<byte[]> fileUpload(
@RequestParam(name ="A",required = false) MultipartFile multipartfile1,
@RequestParam(name ="B",required = false) MultipartFile multipartfile2,
@RequestParam(name ="C",required = false) MultipartFile multipartfile3,
@RequestParam(name ="D",required = false) MultipartFile multipartfile4,
@RequestParam(name ="E",required = false) MultipartFile multipartfile5,
@RequestParam(name ="F",required = false) MultipartFile multipartfile6,
@RequestParam(name ="G",required = false) MultipartFile multipartfile7)
throws IOException {
ResponseEntity<byte[]> resp = null;
try {
MultipartFile[] files = {multipartfile1, multipartfile2, multipartfile3, multipartfile4,
multipartfile5, multipartfile6, multipartfile7};
checkifEmptyAndUpdateTheArray(files); //here fails. I want to extract the file present in cache
DateTime start = DateTime.now();
//storing files
nodesetService.storeNodeSet(files); //able to store the files
log.warn("Start: {}", start);
}catch (Exception e) {
log.error("Unexpected exception: ", e);
return createErrorJSONResponse(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
}
Failure: Since the 2nd time, I do not submit anyfile to the endpoint, the files array is empty, and I can only retreive the file using the name in the cache.
private void checkifEmptyAndUpdateTheArray(MultipartFile[] files) {
Arrays.asList(files).stream().forEach(file->{
if(file==null) {
MultipartFile storedFile = nodesetService.getStoredNodeSet(file); //fails here
if(!storedFile.isEmpty()) {
Arrays.asList(files).set(Arrays.asList(files).indexOf(file), storedFile);
}
else {
log.error("NodeSet is absent in the cache");
}
}
});
NodeSetService.java:
public MultipartFile getStoredNodeSet(MultipartFile file) {
Filter invFilter = new Filter();
invFilter.byType(file.getName()); // possibility to extract file from cache. But fails as file is null
return null;
Any suggestions how to handle this scenario?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
