MultipartFile.transferTo() is a method in Spring Framework's MultipartFile interface that allows a user to save the contents of the uploaded file to a given destination file. This method takes a File object as its argument, which is the destination file where the contents of the uploaded file will be saved.

Here is an example of how to use this method:

@PostMapping("/uploadFile")
public String uploadFile(@RequestParam("file") MultipartFile file) {
    try {
        // Create a new File object as the destination file
        File destFile = new File("C:/uploads/" + file.getOriginalFilename());
        
        // Save the contents of the uploaded file to the destination file
        file.transferTo(destFile);
        
        return "File uploaded successfully!";
    } catch (Exception e) {
        return "Error uploading file: " + e.getMessage();
    }
}

In this example, the uploaded file is saved to the "C:/uploads/" directory with the same name as the original file name. The transferTo() method is used to transfer the contents of the uploaded file to the destination file. If there is an error during the transfer, an exception is caught and an error message is returned.

MultipartFiletransferTo

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

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