[英]How to fix “Required request part 'file' is not present” error when uploading image file
I have a problem when I try to send an (image) file from html to my spring controller, I did everything as same as when I send any other value but it keeps throwing this exact same error... 当我尝试从html发送一个(图像)文件到我的spring控制器时,我遇到了一个问题,我所做的一切与发送任何其他值时相同,但是它不断抛出此完全相同的错误...
HTML 的HTML
<form th:action="@{/changeLogo}" enctype="multipart/form-data" method="post">
<div class="form-group">
<input type="file" name="file"/>
</div>
<button type="submit" class="text-center btn btn-primary"> Upload </button>
</form>
Controller: 控制器:
@PostMapping(value = "/changeLogo")
public String changelogo(@RequestParam("file") MultipartFile file, Principal principal){
SystemUser systemUser =
systemUserService.findByUsername(principal.getName());
Company company = systemUser.getCompany();
try {
System.out.println("Picture exists:" + file);
company.setLogo(file.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
companyRepository.saveAndFlush(company);
return "redirect:/systemUser";
}
Change 更改
public String changelogo(@RequestParam("file") MultipartFile file, Principal principal)
to 至
public String changelogo(MultipartHttpServletRequest file)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.