Java spring: best way to convert a File to a MultipartFile
InputStream stream = new FileInputStream(file)
multipartFileToSend = new MockMultipartFile("file", file.getName(), MediaType.TEXT_HTML_VALUE, stream);
try this
How to convert a multipart file to File?
You can get the content of a MultipartFile
by using the getBytes
method and you can write to the file using Files.newOutputStream()
:
public void write(MultipartFile file, Path dir) {
Path filepath = Paths.get(dir.toString(), file.getOriginalFilename());
try (OutputStream os = Files.newOutputStream(filepath)) {
os.write(file.getBytes());
}
}
You can also use the transferTo method:
public void multipartFileToFile(
MultipartFile multipart,
Path dir
) throws IOException {
Path filepath = Paths.get(dir.toString(), multipart.getOriginalFilename());
multipart.transferTo(filepath);
}
How to convert Image to multipart file in spring
I would suggest convert the image by using this code
FileInputStream input = new FileInputStream(fileItem);
MultipartFile multipartFile = new MockMultipartFile("fileItem",
fileItem.getName(), "image/png", IOUtils.toByteArray(input));
If you would like to use CommonsMultipartFile, I think you should have into your pom file the commons-fileupload
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
The documentation of CommonsMultipartFile states that
NOTE: As of Spring 2.0, this class requires Commons FileUpload 1.1 or
higher. The implementation does not use any deprecated FileUpload 1.0
API anymore, to be compatible with future Commons FileUpload releases.
http://docs.spring.io/spring-framework/docs/2.0.8/api/org/springframework/web/multipart/commons/CommonsMultipartFile.html
Let me know if this worked for you
MultipartFile issue, unable to convert to File
getBytes()
tries to load the whole byte array into memory which is causing your OOM
what you need to do is stream the file and write it out.
Try the following:
private static Path convert(MultipartFile file) throws IOException {
Path newFile = Paths.get(file.getOriginalFilename());
try(InputStream is = file.getInputStream();
OutputStream os = Files.newOutputStream(newFile))) {
byte[] buffer = new byte[4096];
int read = 0;
while((read = is.read(buffer)) > 0) {
os.write(buffer,0,read);
}
}
return newFile;
}
I changed your method to return a Path
instead of File
which is part of the java.nio
package. The package is preferred over java.io
as its been optimized more.
If you do need a File
object you can call newFile.toFile()
Since it returns a Path
object you can use the java.nio.file.Files
class to relocate the file to your preferred directory once it has been written out
private static void relocateFile(Path original, Path newLoc) throws IOException {
if(Files.isDirectory(newLoc)) {
newLoc = newLoc.resolve(original.getFileName());
}
Files.move(original, newLoc);
}
Java Convert MultipartFile to File for large files
Does MultipartFile
belong to Spring's package org.springframework.web.multipart
? If so, you could do
public File convert(MultipartFile file) throws IOException {
File convFile = new File(file.getOriginalFilename());
convFile.createNewFile();
try(InputStream is = file.getInputStream()) {
Files.copy(is, convFile.toPath());
}
return convFile;
}
Related Topics
How to Pass JavaScript Values to Scriptlet in Jsp
How to Get the Part After the Decimal Point in Java
How to Test Code Dependent on Environment Variables Using Junit
Java.Lang.Illegalargumentexception: Not a Managed Type: Class
Spring Boot JPA Unknown Column in Field List
How to Mock a Generic Parameter for a Unit Test in Java
How to Subtract a Constant Value from All Elements of an Array
How to Format a String Number to Have Commas and Round
Springboot Data - No Property Registration Found
How to Store User Input into an Existing Array
How to Join Results of Multiple Tables in Spring JPA Repository
How a Jar File Can Read an External Properties File
Spring Data JPA and Parameters That Can Be Null
On Selenium Webdriver How to Get Text from Span Tag
How to View Apache Parquet File in Windows
Check Date Between Two Other Dates Spring Data Jpa
Netbeans Tomcat Build Failed - the Module Has Not Been Deployed