0

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.

  • Disable file upload in Spring Boot (so it doesn't use the `MultipartResolver`) and upload streaming as shown in this question (and the answer shows how to disable the regular multipart file handling). https://stackoverflow.com/questions/32782026/springboot-large-streaming-file-upload-using-apache-commons-fileupload – M. Deinum Apr 20 '23 at 09:30
  • Thanks so much @M.Deinum although after using HttpServletRequest I'm failing to pass request from the post man I'm getting this error – Janeth Jackson Apr 20 '23 at 15:03
  • Required request parameter 'file' for method parameter type HttpServletRequest is not present – Janeth Jackson Apr 20 '23 at 15:04
  • You shouldn't inject those properties anymore, have you read the question and the solution of the linked answer, or just simply disabled file upload and left everything as is? – M. Deinum Apr 21 '23 at 07:54

0 Answers0