Java spring: convert a file to Multipartfile

Asked

Viewed 1,396 times

0

I created a simple file:

File file = new File("text.txt");

But I would like to convert this to a Multipartfile, which would be the best way?

I have tried the code below without success:

FileInputStream input = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));

Thank you.

  • I think that that can help you

  • A primeira resposta já era o que eu tinha tentado, a segunda não funcionou também, apresentando o erro: (org.apache.commons.fileupload.FileItem) in CommonsMultipartFile cannot be applied to (org.apache.tomcat.util.http.fileupload.disk.DiskFileItem)

1 answer

1


I found a way to do that:

File file = new File("text.txt");
InputStream stream =  new FileInputStream(file)
MultipartFile multipartFileToSend = new MockMultipartFile("file", file.getName(), MediaType.TEXT_HTML_VALUE, stream);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.