From MultiPartFile to Reader object
[wp_ad_camp_5]
The parse method only accepts a single java.io.Reader object and you may need to convert the MultipartFile object to a Reader object. The MultipartFile class has the getInputStream() whose return value can be passed to the constructor of the ImputStreamReader class. The an object of type ImputStreadReader is passed to the parse method.
1 2 3 4 5 6 7 8 | @RequestMapping(value="/importcsv", method=RequestMethod.POST) @ResponseBody public ResponseEntity<MyDTO> importCsv(@RequestParam("file") MultipartFile file) { CSVParser records = CSVFormat.EXCEL.withHeader().parse(new InputStreamReader(file.getInputStream())); ... ResponseEntity<EmployeeDetailDTO> dto = new ResponseEntity<EmployeeDetailDTO>(new EmployeeDetailDTO(),HttpStatus.ACCEPTED); return dto; } |
A multipart resolver is required to make this work. Place the following codes in you @Configuration class.
[wp_ad_camp_2]
1 2 3 4 5 6 7 | @Bean(name = "multipartResolver") public CommonsMultipartResolver createMultipartResolver() { CommonsMultipartResolver resolver=new CommonsMultipartResolver(); resolver.setDefaultEncoding("utf-8"); resolver.setMaxUploadSize(1048576); // 1MB max return resolver; } |