SoFunction
Updated on 2025-03-02

4 ways to convert MultipartFile type to File type in java

Preface

Converting multipartfile to file Java is a common requirement, especially in scenarios where files are uploaded and processed. In Java, MultipartFile is a data structure provided by the Spring framework for representing uploaded files, and File is a class used in the Java standard library to represent files. When encountering this problem during development, I will leave a simple note.

The difference between MultipartFile and File

MultipartFileIs an interface in the Spring framework that represents files uploaded in HTTP requests. It provides methods to access file content, file name, file size, etc.

FileIt is a class in the Java standard library that represents files. It provides methods such as file path, whether the file is readable, and whether the file is writable.

The main difference between MultipartFile and File is their data source and how they operate.

The file represented by MultipartFile is uploaded via HTTP request, while the file represented by File is a file in the local file system. Therefore, we usually need to convert the MultipartFile to File for use in subsequent operations.

How to convert MultipartFile to File

In Java, we can convert MultipartFile to File through the following two methods.

Method 1: Save MultipartFile as a temporary file

First, we can save the MultipartFile as a temporary file and then convert the temporary file to a File.

import ;

public File convertMultipartFileToFile(MultipartFile multipartFile) throws IOException {
    File file = ((), null);
    ((), file);
    return file;
}

The implementation of this method is very simple. We first create a temporary file, use the getOriginalFile method of MultipartFile to get the file name, and use the method to create the file. We then use the method to copy the contents of the MultipartFile into a temporary file. Finally, we return this temporary file.

Method 2: Use Commons IO library

import ;
import ;

public File convertMultipartFileToFile(MultipartFile multipartFile) throws IOException {
    File file = new File(());
    (file, ());
    return file;
}

The implementation of this method is also very simple. We first create a File object, use the getOriginalFilename method of MultipartFile to get the file name, and pass it as a parameter to the constructor of the File. Then, we use methods to write the contents of the MultipartFile into the File object. Finally, we return this File object.

Method 3: Create a temporary path, get the File after conversion, and then delete it

File file = new File(path); 
 
((), file);  

Method 4

public File transferToFile(MultipartFile multipartFile) {
// Choose to use a buffer to achieve this conversion, that is, temporary files created using java. Use the () method.        File file = null;
        try {
            String originalFilename = ();
            String[] filename = ("\\.");
            file=(filename[0], filename[1] + ".");
            (file);
            ();
        } catch (IOException e) {
            ();
        }
        return file;
    }

Attachment: Convert from File type to MultipartFile type

Implementation method

To convert a file of File type to a MultipartFile type, we can use the DiskFileItem class provided by the commons-fileupload library. This class can convert File type files to MultipartFile types. Here is a sample code:

import ;
import ;
import ;
import ;

import ;
import ;

public MultipartFile fileToMultipartFile(File file) throws IOException {
    DiskFileItem fileItem = new DiskFileItem("file", "multipart/form-data", false, (), (int) (), ());
    try {
        (file, ());
    } catch (IOException e) {
        throw new IOException("Failed to convert File to MultipartFile", e);
    }
    return fileItem;
}

In this code, we first create a DiskFileItem object, set the file name and file size and other information, and copy the File type file into the output stream of the object. Finally, this DiskFileItem object is returned, which implements the MultipartFile interface and can be used for file upload operations.

Example of usage

Below is a simple example code that demonstrates how to convert a File type file to a MultipartFile type and upload the file.

import ;

import ;
import ;

public class FileToMultipartFileExample {

    public static void main(String[] args) throws IOException {
        File file = new File("");
        MultipartFile multipartFile = fileToMultipartFile(file);
        
        // Perform file upload operation        // ...
    }
}

In this example, we first create a File object, then call the fileToMultipartFile method defined earlier to convert it to the MultipartFile type, and finally use this MultipartFile object for file upload operation.

Summarize

This is the article about 4 ways to convert MultipartFile type to File type in Java. For more related contents to convert MultipartFile to File type, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!