Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any efficient method in springboot to upload large files? my current api is taking long time to upload large document

I am trying to upload a file of size 5gb in SpringBoot but it takes hours to let request get inside controller from postman. once request get inside controller than it doesn't take much time to upload the document. sometimes it returns connection timeout issue. can anyone provide an efficient way to get the task done in SpringBoot?

here is my controller

@RequestMapping(
        value = "/store",
        method = RequestMethod.POST,
        consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
        produces = MediaType.APPLICATION_JSON_VALUE)
public FilesDTO storeFile(
        @RequestHeader("Authorization") String auth,
        @RequestParam("filename") String filename,
        @RequestParam("collectorId") String collectorId,
        @RequestParam(name = "description", required = false) String description,
        @RequestParam("FileType") DataFileType collectorFileType,
        @RequestPart("file") MultipartFile file)
        throws PRValidationException, IOException {

    return collectorFilesService.storeFile(auth, filename, collectorId, description, collectorFileType, file);
}

here is my Implemention

    @Override
public CollectorFilesDTO storeCollectorFile(String auth, String filename, String collectorId, String description, DataFileType collectorFileType, MultipartFile file) throws IOException, PrValidationException {
    dataServiceApi.findCollectorById(auth,collectorId);
    if (file.isEmpty()) {
        throw new FileNotFoundException(environment.getProperty("pr.validation.file_not_found"));
    }
    if(collectorFileType == null){
        throw new PrValidationException(environment.getProperty("pr.validation.collectorFileType.not.null"));
    }
    return storeFile(auth, filename, collectorId,description,collectorFileType ,file.getBytes());
}

@Override
public CollectorFilesDTO storeFile(String auth, String filename, String collectorId,String description,DataFileType collectorFileType, byte[] bytes) throws IOException, PrValidationException {


    logger.info("UPLOAD_FOLDER=" + UPLOAD_FOLDER_PATH);
    logger.info("fileName=" + filename);

    String absolutePath = UPLOAD_FOLDER_PATH;

    logger.info("Writing " + absolutePath + filename);

    final File parentDirectory = new File(absolutePath);
    if (!parentDirectory.exists()) {
        if (!parentDirectory.mkdirs()) {
            throw new IOException(environment.getProperty("pr.validation.file_not_created"));
        }
    }

    File file = new File(parentDirectory, filename);
    boolean fileAlreadyExists = file.exists();

    try {
        Files.write(Paths.get(absolutePath + filename), bytes);

        CollectorFileEntity collectorFileEntity = storeDocument(auth, filename,collectorId,  description,collectorFileType ,fileAlreadyExists);

        logger.info("file write success and document has been saved in {}ms", System.currentTimeMillis());
        return CollectorFileMapper.INSTANCE.toDto(collectorFileEntity);
    } catch (IOException e) {
        logger.info(e.getMessage());
        throw new IOException(environment.getProperty("pr.validation.write_file_exception"));
    }
}
like image 571
Ultivic Avatar asked Dec 14 '25 19:12

Ultivic


1 Answers

The Apache Commons FileUpload is a popular tool for this use case, here's a guide of how to use it.

like image 183
Johan Nordlinder Avatar answered Dec 16 '25 08:12

Johan Nordlinder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!