Reputation: 41
I have trouble with convert type "multipart"
"Failed to convert property value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'java.lang.String' for property 'foto'; 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 'foto': no matching editors or conversion strategy found"
my Code : Controller
@RequestMapping(value = "/desainkartu/action.insert", method = RequestMethod.POST)
public String actionInsert(@Valid @ModelAttribute("data") DesainKartu data,
BindingResult bindingResult,
Model model,
Principal principal,
@RequestParam("files") MultipartFile[] files) throws IOException {
StringBuilder fileNames = new StringBuilder();
String url = "desainkartu/insert";
try {
if(bindingResult.hasErrors()) {
StringBuffer errorMessage = new StringBuffer();
List<FieldError> aList = bindingResult.getFieldErrors();
for(FieldError bList : aList) {
errorMessage.append("<div>"+bList.getDefaultMessage()+"</div>");
url = "desainkartu/insert";
}
for(MultipartFile file : files) {
Path fileNameAndPath = Paths.get(uploadDirectory, file.getOriginalFilename());
fileNames.append(file.getOriginalFilename());
Files.write(fileNameAndPath, file.getBytes());
}
model.addAttribute("message", errorMessage.toString());
} else {
try {
data.setCreatedby(principal.getName());
data.setCreateddate(date1);
service.save(data);
url = "redirect:/desainkartu/list?status=true";
} catch (Exception e) {
StringBuilder ste = new StringBuilder();
ste.append(e.getMessage());
for(StackTraceElement element : e.getStackTrace()) {
ste.append(element.toString()+"\n");
}
log.error(ste.toString());
model.addAttribute("message", ste.toString());
}
}
properties
spring.http.multipart.max-file-size=15MB
spring.http.multipart.max-request-size=15MB
multipart.enabled=true
thanks
Upvotes: 4
Views: 7060
Reputation: 221
This is the way I fix this, you can try
about entity :
public byte[] getImage() {
return image;
}
in thymeleaf form, at type="file" input we no need to add th:field="*{image}" Just need to do like this
<div class="form-group input-group has-feedback">
<label class="col-form-label">Image</label>
<input type="file" name="file" />
</div>
and in the controller
@PostMapping("/register")
public String createAccount(@Valid @ModelAttribute("user") User user, Model
model, HttpServletRequest request,final @RequestParam MultipartFile file,
RedirectAttributes redirectAttr) throws IOException {}
Upvotes: 1