我有以下实体。
package com.example.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import java.util.Date;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.springframework.data.annotation.CreatedDate;
@Entity
@Table(name = "documents")
public class Documents {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "date_created", nullable = false, updatable = false)
@CreatedDate
private Date date_created;
@Column(name = "name")
private String name;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "type_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private Documenttypes typeid;
@Column(name = "file")
private String file;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getDate_created() {
return date_created;
}
public void setDate_created(Date date_created) {
this.date_created = date_created;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Documenttypes getTypeid() {
return typeid;
}
public void setTypeid(Documenttypes typeid) {
this.typeid = typeid;
}
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
}
其中一个名为file的字段用于上传文档。控制器的功能如下:
public ModelAndView add(@ModelAttribute("documentsForm") Documents documents,@RequestParam("file") MultipartFile file)
{
//upload
if (file.isEmpty()) {
return new ModelAndView("redirect:/document/list");
}
try {
byte[] bytes = file.getBytes();
Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
documents.setFile("test");
Files.write(path, bytes);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//end
documentsService.addDocument(documents);
return new ModelAndView("redirect:/document/list");
}
但然后我尝试上传文件并提交一个表单,我得到这个异常未能转换类型的属性值'org.springframework.web.multipart.support。StandardMultipartHttpServletRequest$StandardMultipartFile'到所需的类型'java.lang。属性'file'的字符串';嵌套异常是java.lang.IllegalStateException: Cannot convert type 'org.springframewo的值
完整错误是:
2021-10-15 12:44:35.029 WARN 13500 --- [nio-8888-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'documentsForm' on field 'file': rejected value [org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@2c2feacc]; codes [typeMismatch.documentsForm.file,typeMismatch.file,typeMismatch.java.lang.String,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [documentsForm.file,file]; arguments []; default message [file]]; default message [Failed to convert property value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'java.lang.String' for property 'file'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'java.lang.String' for property 'file': no matching editors or conversion strategy found]]
@ModelAttribute
将启用HTTP请求到@ModelAttribute
实例的数据绑定。
它将尝试将查询参数的值,表单数据的字段名和其他(参见此)绑定到@ModelAttribute
实例的字段,前提是它们的名称匹配。
在您的示例中,由于您的控制器方法有一个名为file的查询参数,它将尝试将其值绑定到Documents
的file字段。由于文件查询参数为MultipartFile
类型,而Documents
为String
类型,因此需要进行转换,但没有注册Converter
来进行转换。
不确定是否真的要将MultipartFile
的内容绑定到Documents
的文件字段。如果不匹配,您可以简单地重命名其中一个,使它们不匹配。
否则,您可以实现Converter
来将MultipartFile
转换为String
:
public class MulitpartConverter implements Converter<MultipartFile, String> {
@Override
public String convert(MultipartFile source) {
try {
return new String(source.getBytes(), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException("Fail to convert multipart file to string", e);
}
}
}
并注册:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new MulitpartConverter());
}
}