Click here to Skip to main content
15,746,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

What I have tried:

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.
Posted
Comments
Gerry Schmitz 20-Apr-23 10:32am    
Is it "chunking" so you don't have to restart the whole thing if there is an issue?
Office Systems 21-Apr-23 7:49am    
No its not chunking @Gerry Schmitz

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900