是的,您可以简单地通过包装类来实现。
1)创建一个
Class保存表单数据:
public class FormWrapper { private MultipartFile image; private String title; private String description;}
2)创建
form用于提交数据的HTML :
<form method="POST" enctype="multipart/form-data" id="fileUploadForm" action="link"> <input type="text" name="title"/><br/> <input type="text" name="description"/><br/><br/> <input type="file" name="image"/><br/><br/> <input type="submit" value="Submit" id="btnSubmit"/></form>
3)创建一种接收表单
text数据和
multipart文件的方法:
@PostMapping("/api/upload/multi/model")public ResponseEntity<?> multiUploadFileModel(@ModelAttribute FormWrapper model) { try { // Save as you want as per requiremens saveUploadedFile(model.getImage()); formRepo.save(mode.getTitle(), model.getDescription()); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } return new ResponseEntity("Successfully uploaded!", HttpStatus.OK);}
4)保存方法
file:
private void saveUploadedFile(MultipartFile file) throws IOException { if (!file.isEmpty()) { byte[] bytes = file.getBytes(); Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename()); Files.write(path, bytes); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)