1

I have a folder 'C:\data' with a lot of PDF files and I want to convert those files to CommonsMultipartFile spring class.

In my project, I am using spring class CommonsMultipartFile in order to read file from the request and manipulate them. I prefer to reuse this project also for Files - java class.

I did:

File folder = new File(path);
File[] listOfFiles = folder.listFiles();

and I want to convert to: List of CommonsMultipartFile;

Thanks!!!

  • List of CommonsMultipartFiles? Here is an article on how to convert it: https://stackoverflow.com/questions/16648549/converting-file-to-multipartfile – Beri Oct 21 '18 at 09:18
  • I read this answer before. I want to find a way to do it elegant for folder with more than 1000 files. those example do the conversion per file – e leibowitz Oct 21 '18 at 09:26
  • In my understanding you have a wrong approach here. In your Controller, you receive CommonsMultipartFile, but you pass a File or an byteStream into your service. In that way you can create a second test/application, that uses the same service, but passes Files from the drive. You can commit the controller this way, as it is only a layer to transmit data to your service, it should not contain any reusable logic, only delegating data to other components. – Beri Oct 21 '18 at 17:14
  • I will explain the situation: I have a controller that contains files in the request and I parse it to list of CommonsMultipartFile. after I am using some utils function on the data that works on CommonsMultipartFile. I want to do the same process also for files from drive. this is the reason that I want to convert files to CommonsMultipartFile. – e leibowitz Oct 22 '18 at 10:47
  • Why do you need CommonsMultipartFile instead of MultipartFile? As for an elegant way streaming would do Arrays.stream(folder.listFiles()).map(this::mapToMultipartFile).forEach(this::processFile), when you are processing everything as a stream you will work with a single file at a time, so no matter how many you have, it will not matter. – Beri Oct 22 '18 at 10:59

0 Answers0