Is it possible to process Multipart CSV File uploaded from web application using Spring Batch?

Viewed 1341

I want to convert csv file contains student data to List and insert into database. I have gone through many examples from various sources, none of them provides answer for batch processing csv files uploaded from Web UI using spring batch. All examples process csv files located in resource folder (classpath). Please help me to process csv files uploaded using Post Method as below.

@PostMapping("/save")
public ResponseEntity saveStudentDetails(@RequestParam("file") MultipartFile studentCSV) {
   //code to initate batch processing for studentCSV file
}

Thanks in Advance.

1 Answers

Controller

@RequestMapping(method = RequestMethod.POST, value = "/save")
        public ReturnFormat uploadCSV(@RequestParam("files")  MultipartFile file )
        {
            return uploadingService.uploadCSV( file );
        }

Service class will be like

    public void uploadCSV (MultipartFile multipartFile)
    {
        ReturnFormat rf = new ReturnFormat();
        SuccessErrorList selist = new SuccessErrorList();
        try
        {
            File file = convertMultiPartToFile( multipartFile );

    }

    private File convertMultiPartToFile( MultipartFile file ) throws IOException
    {
        File convFile = new File( file.getOriginalFilename() );
        FileOutputStream fos = new FileOutputStream( convFile );
        fos.write( file.getBytes() );
        fos.close();
        return convFile;
    }
Related