简体   繁体   English

上传图片文件时如何解决“所需的请求部分'文件'不存在”错误

[英]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.

相关问题 出现“不存在所需的请求部分&#39;文件&#39;”错误 - Getting “Required request part 'file' is not present” error Spring Thymeleaf所需的请求部分“文件”不存在 - Spring Thymeleaf Required request part 'file' is not present Tomcat:所需的请求部分“文件”不存在 - Tomcat : Required request part 'file' is not present Spring Boot Required 请求部分“文件”不存在 - Spring Boot Required request part 'file' is not present Spring 文件上传 - “所需的请求部分不存在” - Spring File Upload - 'Required request part is not present' MissingServletRequestPartException:所需的请求部分“文件”不存在 Springboot - MissingServletRequestPartException: Required request part 'file' is not present Springboot 如何修复 Spring 引导分段上传中的“所需请求部分不存在” - How to fix "Required request part is not present" in a Spring Boot multipart upload 上传文件springboot所需的请求部分“文件”不存在 - upload file springboot Required request part 'file' is not present 上传文件时不存在必需的MultipartFile参数“文件” - Required MultipartFile parameter 'file' is not present when uploading file 所需的请求部分“文件”不存在。 尝试上传图像,角度-&gt;春天 - Required request part 'file' is not present. Trying upload an image, angular -> spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM