Home > other >  Java converts MultipartFile File for large files
Java converts MultipartFile File for large files

Time:11-26

I'm using the following method converts MultipartFile file:

The public File convert (MultipartFile File) throws IOException {
The File convFile=new File (File. GetOriginalFilename ());
ConvFile. CreateNewFile ();
FileOutputStream fos=new FileOutputStream (convFile);
Fos. Write (file. GetBytes ());
fos.close();
Return convFile;
}

Which job no problem but for large file I got the exception:

Java. Lang. OutOfMemoryError: Java heap space

I added more heap but there are still errors.
So whether there is a way of programming to solve this problem, it may in the transformation will be more file is split into smaller pieces, but I'm not sure how to encode it.
Any help or advice will be appreciated.

CodePudding user response:

MultipartFile whether belong to Spring package org. Springframework. Web. Multipart? If so, you can do it

The public File convert (MultipartFile File) throws IOException {
The File convFile=new File (File. GetOriginalFilename ());
ConvFile. CreateNewFile ();
Try (InputStream is=file. GetInputStream ()) {
Files. The copy (is, convFile toPath ());
}
Return convFile;
}

CodePudding user response:

references of the building Lord reply:
I'm using the following method converts MultipartFile file:

The public File convert (MultipartFile File) throws IOException {
The File convFile=new File (File. GetOriginalFilename ());
ConvFile. CreateNewFile ();
FileOutputStream fos=new FileOutputStream (convFile);
Fos. Write (file. GetBytes ());
fos.close();
Return convFile;
}

Which job no problem but for large file I got the exception:

Java. Lang. OutOfMemoryError: Java heap space

I added more heap but there are still errors.
So whether there is a way of programming to solve this problem, it may in the transformation will be more file is split into smaller pieces, but I'm not sure how to encode it.
Any help or advice will be appreciated.



Brothers, have to solve this problem?
  • Related