The simplest mutual conversion between java MultipartFile and File

The difference between MultipartFile and File

MuitipartFile is an interface used to process file uploads in the Spring framework. It encapsulates the information of file uploads, such as file name, file type, etc.

File is a file operation class provided in the Java standard library, which is used to describe file information, such as file path, file size, etc.

In general, MultipantFile is used to process file uploads, while File is used to describe file information.

Mutual conversion between MultipartFile and File

1. MultipartFile to File

The most common way (write via file stream):

public File multipartFile2File (MultipartFile multipartFile) {
    // Create a temporary file
    String path = "export/demo.xlsx";
    File file = new File(path);
    InputStream inputStream = null;
    FileOutputStream outputStream = null;
    try {
        // Get file input stream
        inputStream = multipartFile.getInputStream();
       
        if (!file.exists()) {
            file.createNewFile();
        }
        // create output stream
        outputStream = new FileOutputStream(file);
        byte[] bytes = new byte[1024];
        int len;
        // write to the created temporary file
        while ((len = inputStream.read(bytes)) > 0) {
            outputStream.write(bytes, 0, len);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        // turn off the flow
        try {
            if (outputStream != null) {
                outputStream.close();
            }
            if (outputStream != null) {
                inputStream.close();
            }
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return file;
}

The easiest way:

Use the copy() method of the FileCpopyUtils class in Spring to convert MultipartFile to File type

public File multipartFile2File (MultipartFile multipartFile) {
    String path = "export/demo.xlsx";
    File file = new File(path);
    try {
        if (!file.exists()) {
            file.createNewFile();
        }
        // The bottom layer is also written to the file file through the io stream
        FileCopyUtils.copy(multipartFile.getBytes(), file);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return file;
}
2. File to MultipartFile

Using org.springframework.mock.web.MockMultipartFile needs to import spring-test.jar

public MultipartFile file2MultipartFile () {
    String path = "export/demo.xlsx";
    File file = new File(path);
    MultipartFile multipartFile;
    try {
        FileInputStream fileInputStream = new FileInputStream(file);
        multipartFile = new MockMultipartFile("copy"+file.getName(),file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(),fileInputStream);
        System.out.println(multipartFile.getName()); // 输出demo.xlsx
        fileInputStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return multipartFile;
}

 Use CommonsMultipartFile

public MultipartFile file2MultipartFile () {
    String path = "export/demo.xlsx";
    File file = new File(path);
    MultipartFile multipartFile;
    try {
        DiskFileItem fileItem2 = (DiskFileItem) new DiskFileItemFactory().createItem("file", ContentType.MULTIPART.getValue(), true, file.getName());
        //You can also use IOUtils.copy(inputStream,os);
        Files.copy(Paths.get(file.getAbsolutePath()), fileItem2.getOutputStream());
        multipartFile = new CommonsMultipartFile(fileItem2);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return multipartFile;
}

 Another day of fishing,,,,,

Guess you like

Origin blog.csdn.net/m0_56324585/article/details/131723857