MultipartFile is a class in Java that represents an uploaded file. It is commonly used in web applications to handle file uploads.

MultipartFile provides methods to access the contents and metadata of the uploaded file, such as the file name, size, content type, and input stream. It also provides methods to save the file to the server's file system or to a database.

Here is an example of using MultipartFile in a Spring MVC controller:

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            String fileName = file.getOriginalFilename();
            long fileSize = file.getSize();
            String contentType = file.getContentType();
            
            // Save the file to the server
            try {
                file.transferTo(new File("/path/to/save/" + fileName));
                return "File uploaded successfully!";
            } catch (IOException e) {
                e.printStackTrace();
                return "Failed to upload file!";
            }
        } else {
            return "No file selected!";
        }
    }
}

In this example, the uploadFile method takes a MultipartFile parameter named "file" which represents the uploaded file. The method checks if the file is empty and if not, retrieves its name, size, and content type. It then saves the file to the server's file system using the transferTo method.

Note that MultipartFile is part of the Spring framework and is commonly used with Spring MVC or Spring Boot applications

MultipartFile

原文地址: https://www.cveoy.top/t/topic/hG6g 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录