SoFunction
Updated on 2025-04-10

How to convert Java backend local files to MultipartFile type implementation

Convert Java backend local file to MultipartFile type

The front-end upload file is converted to MultipartFile type, and there is no need to say more about the Java background receiving and automatically converting it through springmvc.

Now, what should I do if I need to convert the local file to the MultipartFile type in the background java code?

The following code

Mainly, FileItem is created using FileItemFactory. The implementation class of MultipartFile CommonsMultipartFile constructs an object to convert the locally read file stream into MultipartFile.

getMultipartFiles() and getMultipartFiles() support the conversion of one file and the conversion of multiple files.

import ;
import ;
import ;
import org.;
import org.;
import ;
import ;
import ;
 
import .*;
 
public class MultipartFileTest {
 
    private static final Logger log = ();
 
    private MultipartFileTest() { }
 
 
    public static void main(String[] args) {
        //Convert local file to MultipartFile type        try{
            InputStream fis = new FileInputStream("E:\\tool\\");
            (fis, "");
        }catch (FileNotFoundException e){
            ("FileNotFoundException exception", e);
            throw new RuntimeException("The file does not exist");
        }
    }
 
    public static MultipartFile getMultipartFile(InputStream inputStream, String fileName) {
        FileItem fileItem = createFileItem(inputStream, fileName);
        return new CommonsMultipartFile(fileItem);
    }
 
 
    public static MultipartFile[] getMultipartFiles(InputStream[] inputStream, String fileName) {
        // Multi-file conversion        int length = ;
        MultipartFile[] multipartFiles = new MultipartFile[length];
        for (int i = 0; i < length; i++) {
            FileItem fileItem = createFileItem(inputStream[i], fileName);
            multipartFiles[i] = new CommonsMultipartFile(fileItem);
        }
        return multipartFiles;
    }
 
 
    public static FileItem createFileItem(InputStream inputStream, String fileName) {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem fileItem = ("file", MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
        int read = 0;
        OutputStream os = null;
        byte[] buffer = new byte[10 * 1024 * 1024];
        try {
            os = ();
            while ((read = (buffer, 0, 4096)) != -1) {
                (buffer, 0, read);
            }
            ();
        } catch (IOException e) {
            ("os write exception", e);
            throw new IllegalArgumentException("File stream output failed");
        } finally {
            if (os != null) {
                try {
                    ();
                } catch (IOException e) {
                    ("stream os close exception", e);
                }
            }
            if (inputStream != null) {
                try {
                    ();
                } catch (IOException e) {
                    ("stream inputStream close exception", e);
                }
            }
        }
        return fileItem;
    }
}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.