"MultipartFile" is a class in Java that represents an uploaded file. It is commonly used in web applications to handle file uploads. \n\nMultipartFile 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.\n\nHere is an example of using MultipartFile in a Spring MVC controller:\n\njava\nimport org.springframework.web.multipart.MultipartFile;\nimport org.springframework.web.bind.annotation.PostMapping;\nimport org.springframework.web.bind.annotation.RequestParam;\nimport org.springframework.web.bind.annotation.RestController;\n\n@RestController\npublic class FileUploadController {\n\n @PostMapping("/upload")\n public String uploadFile(@RequestParam("file") MultipartFile file) {\n if (!file.isEmpty()) {\n String fileName = file.getOriginalFilename();\n long fileSize = file.getSize();\n String contentType = file.getContentType();\n \n // Save the file to the server\n try {\n file.transferTo(new File("/path/to/save/" + fileName));\n return "File uploaded successfully!";\n } catch (IOException e) {\n e.printStackTrace();\n return "Failed to upload file!";\n } \n } else {\n return "No file selected!";\n }\n }\n}\n\n\nIn 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.\n\nNote that MultipartFile is part of the Spring framework and is commonly used with Spring MVC or Spring Boot applications.


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

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