I have a Java Spring MVC web application. From the client, through AngularJS, I upload the file and send it to the Controller as webservice.
In my controller, I get it as a MultipartFile , and I can copy it to the local machine.
But I want to upload the file to Amazone S3 . So I have to convert it to java.io.File . Now I do this, I copy it to the local machine, and then upload it to S3 using jets3t .
Here is my way to convert to controller
MultipartHttpServletRequest mRequest=(MultipartHttpServletRequest)request; Iterator<String> itr=mRequest.getFileNames(); while(itr.hasNext()){ MultipartFile mFile=mRequest.getFile(itr.next()); String fileName=mFile.getOriginalFilename(); fileLoc="/home/mydocs/my-uploads/"+date+"_"+fileName;
Then I use FIleCopyUtils from SpringFramework
File newFile = new File(fileLoc); // if the directory does not exist, create it if (!newFile.getParentFile().exists()) { newFile.getParentFile().mkdirs(); } FileCopyUtils.copy(mFile.getBytes(), newFile);
Thus, it will create a new file on the local computer. This file that I use in S3
S3Object fileObject = new S3Object(newFile); s3Service.putObject("myBucket", fileObject);
It creates a file on my local system. I do not want to create.
Without creating a file on the local system, how to convert MultipartFIle to java.io.File?
java spring-mvc file-io multipart
Shiju K Babu
source share