【问题标题】:Error upload file with Spring Security and Thymeleaf使用 Spring Security 和 Thymeleaf 上传文件时出错
【发布时间】:2014-07-17 23:14:24
【问题描述】:

在我的情况下,我有一个带有 Spring Data 的控制器类和 Spring MVC 的验证器,它们使用 Thymeleaf 2.1.1 制作的模板处理表单数据

我使用 Spring Framework 3.2.8 和 Spring Security 3.2.3。

控制器

@RequestMapping(value = "/save", method = {RequestMethod.POST, RequestMethod.GET })
public String save(@RequestParam("curriculumVitae") MultipartFile curriculumVitae, @Valid @ModelAttribute("user") User user, BindingResult result, Model model, Pageable pageable) {

    try {
        user.setCurriculumVitae(curriculumVitae.getBytes());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    userRepository.save(user);

    return "/crudUser";

查看

                <form id="myform" 
                action="#" 
                th:action="@{${url}+'/save'+'?' + ${_csrf.parameterName} + '=' + ${_csrf.token}}"
                th:object="${user}"
                th:method="post"
                enctype="multipart/form-data">

                <div class="col-sm-2 col-lg-2">
                    <div th:class="form-group" th:classappend="${#fields.hasErrors('curriculumVitae')}? has-error"> 
                        <label class="control-label" scope="col"
                            th:text="#{crudUser.curriculumVitae}">Curriculum Vitae</label>
                            <input class="form-control input-sm" type="file" th:field="*{curriculumVitae}" name="curriculumVitae" />
                    </div>
                </div>

                </form>

我的日志中显示的错误是:

Failed to convert property value of type org.springframework.web.multipart.commons.CommonsMultipartFile to required type byte[] for property curriculumVitae; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile] to required type [byte] for property curriculumVitae[0]: PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned inappropriate value of type [org.springframework.web.multipart.commons.CommonsMultipartFile]

提前感谢谁会帮助我。

【问题讨论】:

  • 请发布完整的堆栈跟踪和您的 spring 配置

标签: spring jakarta-ee spring-mvc spring-security thymeleaf


【解决方案1】:

已解决!

在堆栈跟踪中,我忘记在我的 java 配置文件中的 MultiPartResolver 中将 maxUploadSize 设置为比默认值更高的值。

  @Bean
   public MultipartResolver multipartResolver() {
   CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
   multipartResolver.setMaxUploadSize(500000000);
   return multipartResolver;
}

【讨论】:

  • 你使用的是xml还是Java Config?
猜你喜欢
  • 2015-11-09
  • 1970-01-01
  • 2019-11-25
  • 2018-07-08
  • 2018-01-22
  • 2015-03-29
  • 2018-12-01
  • 2022-06-14
  • 2021-02-23
相关资源
最近更新 更多