我正在使用 Spring Boot 使用 Spring Data-Jpa。我有一个多部分表单将文件上传到数据库,其中包含附加字段,例如(名字、姓氏、电子邮件、简历等)但我不断收到此错误消息,用于将多部分文件转换为所需的字节 [] 类型。如何将 pdf 文件从多部分文件转换为 byte[] 类型?
这是我的控制器类
@RequestMapping(value = "/jobs/applyJob/{id}", method = RequestMethod.POST)
public String handleFormSubmit(@PathVariable Long id, @ModelAttribute @Valid Candidate candidate,
BindingResult bindingResult, @RequestParam("resume") MultipartFile file,
RedirectAttributes redirectAttributes) {
Job job = jobService.findJob(id);
candidate.setJob(job);
if (file.isEmpty()) {
return null;
}
try {
byte[] content = file.getBytes();
Path path = Paths.get(UPLOAD_FOLDER + file.getOriginalFilename());
Files.write(path, content);
} catch (IOException e) {
e.printStackTrace();
}
if (bindingResult.hasErrors()) {
return "applyJob";
}
redirectAttributes.addFlashAttribute("message", "Job applied !");
return "redirect:/jobs";
}
我的实体:
@Entity
@Table(name = "candidates")
public class Candidate {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotEmpty
@Column(name = "first_name", length = 50)
private String firstName;
@NotEmpty
@Column(name = "last_name", length = 50)
private String lastName;
@NotEmpty
@Column(name = "email", length = 100)
private String email;
@NotEmpty
@Column(name = "phone", length = 15)
private String phone;
@NotEmpty
@Column(name = "address", length = 255)
private String address;
@NotEmpty
@Column(name = "thoughts_on_job", length = 255)
private String thoughtsOnJob;
@NotEmpty
@Column(name = "resume")
@Lob
private byte[] resume;
我的错误:
无法将 org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile 类型的属性值转换为属性恢复所需的类型 byte[];嵌套异常是 java.lang.IllegalArgumentException:无法将 org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile 类型的值转换为属性恢复 [0] 所需的类型字节:PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor]返回 org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile 类型的不适当值