我有一个简单的表格可以上传文件,这很麻烦,出于安全考虑,我决定实施PRG模式,但是现在我的鳕鱼不起作用了。
@PostMapping("/books")
public String add(@Valid @ModelAttribute("book") Book book, HttpServletResponse response,RedirectAttributes redirectAttributes, BindingResult result, @RequestParam(value = "photo", required = false) MultipartFile photo, Model forExc, @RequestParam(value = "valute") String valute) {
if (result.hasErrors()) return "addBook";
if (!photo.isEmpty()) {
try {
validateImg(photo);
} catch (IOException ex) {
result.reject("uk_UA", "Поганий тип");
return "addBook";
}
}
redirectAttributes.addFlashAttribute("book",book);
redirectAttributes.addAttribute("photo", photo);
redirectAttributes.addAttribute("valute", valute);
redirectAttributes.addFlashAttribute( "success",true);
return "redirect:/valid";
}
@GetMapping("/valid")
public String validations(HttpServletRequest request,Model model, @ModelAttribute("success") boolean success, @ModelAttribute("book") Book book, @RequestParam("valute") String valute, @RequestParam(value = "photo",required = false) MultipartFile photo) {
if (success) {
StringBuilder builder = new StringBuilder();
builder.append(service.download(photo));
Logger.getAnonymousLogger().info("SOMETHING HAS BROKEN IN LOGGING FILE");
System.out.println("INSIDE ADD METHOD ");
book.setFileId(builder.toString());
service.add(book);
return "redirect:/books";
} else {
return "redirect:/index.jsp";
}
}
当我添加一本书时,出现以下异常:
org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'java.lang.String'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'java.lang.String': no matching editors or conversion strategy found