I want to upload large file more than 2gb to a particular folder in spring boot, the challenge I'm facing is that, after file upload from client application (Angular Application/ Postman) it takes more than 50seconds to hit the controller, after debugging I found that when using MultipartFile it uploads the entire file into temp folder afterwards the entire file is copied from the temp folder to the target folder the deleted from the temp folder and thus increases the execution time therefore I want to upload the the entire file direct into the target folder without being copied from the temp folder, how can I achieve this
Controller
@RestController
public class UploadData{
@Autowired
private UploadService uploadService;
@PostMapping("/upload_file")
protected UploadResponse uploadFile(@ModelAttribute UserUpload user, @RequestParam MultipartFile file) throws IOException,SQLException {
return uploadService.uploadFile(user, file);
}
}
Service
@Service
public class UploadService {
@Value(value = "${file.upload.dir}")
String fileUploadDir;
public UploadResponse uploadFile(UserUpload user, MultipartFile file) throws IOException, SQLException {
Path uploadFolder = Paths.get(fileUploadDir);
Path fileUploadPath = uploadFolder.resolve(file.getOriginalFilename());
Files.copy(file.getInputStream(), fileUploadPath, StandardCopyOption.REPLACE_EXISTING);
String basename = FilenameUtils.getBaseName(file.getOriginalFilename());
return new UploadResponse(basename + "Uploaded Successfully");
}else {
return new UploadResponse("Failed To Upload Data Set");
}
}
}
Any suggestion will be much appreciated.