Convert MultipartFile to java.io.File without copying to local machine

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; //date is String form of current date. 

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?

+7
java spring-mvc file-io multipart
source share
2 answers

MultipartFile by default is already saved on your server as a file when the user loads. From now on, you can do whatever you want with this file. There is a method that moves this temporary file to any desired destination. http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/web/multipart/MultipartFile.html#transferTo(java.io.File)

But MultipartFile is just an API, you can implement any other MultipartResolver http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/web/multipart/MultipartResolver.html

This API accepts an input stream and you can do whatever you want with it. The default implementation (usually commons-multipart) saves the temp file as a file.

But another problem remains here - if the S3 API accepts the file as a parameter - you can do nothing with it - you need a real file. If you want to not create files at all, create your own S3 API.

+6
source share

The question has been around for years, so I'm not sure if the jets35 link provided by the OP had the following snippet at that time.

If your data is not File or String , you can use any input stream as a data source, but you must manually set the Content-Length .

 // Create an object containing a greeting string as input stream data. String greeting = "Hello World!"; S3Object helloWorldObject = new S3Object("HelloWorld2.txt"); ByteArrayInputStream greetingIS = new ByteArrayInputStream(greeting.getBytes()); helloWorldObject.setDataInputStream(greetingIS); helloWorldObject.setContentLength( greeting.getBytes(Constants.DEFAULT_ENCODING).length); helloWorldObject.setContentType("text/plain"); s3Service.putObject(testBucket, helloWorldObject); 

Turns out you don't have to create a local file first. Since @Boris offers you to submit a S3Object using Data Input Stream , Content Type and Content Length , you will get from MultipartFile.getInputStream() , MultipartFile.getContentType() and MultipartFile.getSize() respectively.

+5
source share

All Articles