我有以下物體。
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;
}
}
稱為檔案的欄位之一是上傳檔案。控制器功能如下:
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.String” '檔案'; 嵌套例外是 java.lang.IllegalStateException:無法轉換型別 '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]]
uj5u.com熱心網友回復:
@ModelAttribute將啟用從 HTTP 請求到@ModelAttribute實體的資料系結。
它將嘗試將來自查詢引數、表單資料的欄位名稱和其他(請參閱此)的值系結到@ModelAttribute實體的欄位,前提是它們的名稱匹配。
在您的情況下,由于您的控制器方法有一個名為 file 的查詢引數,它會嘗試將其值系結到Documents的 file 欄位。由于檔案查詢引數在MultipartFiletype 但Documents在Stringtype 中,它需要轉換但沒有Converter注冊進行轉換。
不確定您是否真的要將 的內容系結MultipartFile到Documents的檔案欄位。如果不是,您可以簡單地重命名其中任何一個,使它們不匹配。
否則,您可以實作 aConverter轉換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());
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/318588.html
