Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting File to MultiPartFile

Is there any way to convert a File object to MultiPartFile? So that I can send that object to methods that accept the objects of MultiPartFile interface?

File myFile = new File("/path/to/the/file.txt")  MultiPartFile ....?  def (MultiPartFile file) {   def is = new BufferedInputStream(file.getInputStream())   //do something interesting with the stream } 
like image 858
birdy Avatar asked May 20 '13 11:05

birdy


People also ask

How do I change a file to MultipartFile?

MultipartFile#getBytes We can use this method to write the bytes to a file: MultipartFile multipartFile = new MockMultipartFile("sourceFile. tmp", "Hello World". getBytes()); File file = new File("src/main/resources/targetFile.

What is a MultipartFile?

A representation of an uploaded file received in a multipart request. The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired.

What is multipartfile in HTML?

Record it MultipartFile is of spring type and represents the file uploaded in form data mode in HTML, including binary data + file name. [know from Baidu] 1. File to MultipartFile

How to write bytes to a file using multipartfile?

MultipartFile has a getBytes () method that returns a byte array of the file's contents. We can use this method to write the bytes to a file: The getBytes () method is useful for instances where we want to perform additional operations on the file before writing to disk, like computing a file hash. 3. MultipartFile#getInputStream

How to convert a spring multipartfile to a file?

Using the transferTo () method, we simply have to create the File that we want to write the bytes to, then pass that file to the transferTo () method. The transferTo () method is useful when the MultipartFile only needs to be written to a File. 5. Conclusion In this tutorial, we explored ways of converting a Spring MultipartFile to a File.

How to get the content of a multipartfile in Python?

You can get the content of a MultipartFile by using the getBytes method and you can write to the file using Files.newOutputStream (): Path filepath = Paths.get(dir.toString(), file.getOriginalFilename());


1 Answers

MockMultipartFile exists for this purpose. As in your snippet if the file path is known, the below code works for me.

import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import org.springframework.mock.web.MockMultipartFile;  Path path = Paths.get("/path/to/the/file.txt"); String name = "file.txt"; String originalFileName = "file.txt"; String contentType = "text/plain"; byte[] content = null; try {     content = Files.readAllBytes(path); } catch (final IOException e) { } MultipartFile result = new MockMultipartFile(name,                      originalFileName, contentType, content); 
like image 109
Arun Avatar answered Sep 19 '22 14:09

Arun