springboot怎么上传一个照片存放多个地方
时间: 2023-04-10 10:05:18 浏览: 80
您可以使用Spring Boot中的MultipartFile类来处理文件上传。您可以使用以下步骤将照片存储在多个位置:
1. 创建一个MultipartFile对象来处理上传的文件。
2. 使用Java的File类创建一个文件对象,指定要将文件存储到的位置。
3. 使用MultipartFile的transferTo()方法将文件保存到第一个位置。
4. 使用Java的Files类将文件复制到其他位置。
以下是示例代码:
```
@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
// Create a file object for the first location
File firstLocation = new File("/path/to/first/location/" + file.getOriginalFilename());
// Save the file to the first location
file.transferTo(firstLocation);
// Create file objects for the other locations
File secondLocation = new File("/path/to/second/location/" + file.getOriginalFilename());
File thirdLocation = new File("/path/to/third/location/" + file.getOriginalFilename());
// Copy the file to the other locations
Files.copy(firstLocation.toPath(), secondLocation.toPath());
Files.copy(firstLocation.toPath(), thirdLocation.toPath());
return "File uploaded successfully!";
}
```