接收content-type=multipart/form-data的表单数据
java
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.Map;
@RestController
@RequestMapping("/api")
public class FormDataController {
@PutMapping(value = "/submit", consumes = "multipart/form-data")
public String handleFormData(@RequestParam Map<String, String> formData,
@RequestParam(value = "file", required = false) MultipartFile file) {
// 输出表单数据
formData.forEach((key, value) -> System.out.println(key + ": " + value));
// 如果上传了文件,打印文件信息
if (file != null) {
System.out.println("File name: " + file.getOriginalFilename());
}
return "Form data received successfully!";
}
}