Error converting image to String to save to bank

2

I'm trying to save an image to my form in the database, but I'm having trouble resolving the error:

  

Field error in object 'user' on field 'photo': rejected value [org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@2218206a]; codes [typeMismatch.user.photo, typeMismatch.foto, typeMismatch.java.lang.String, typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.photo, photo]; arguments []; default message [photo]]; default message [Failed to convert property of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest $ StandardMultipartFile' to required type 'java.lang.String' for property 'photo'; nested exception is java.lang.IllegalStateException: Can not convert value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest $ StandardMultipartFile' to required type 'java.lang.String' for property 'photo': no matching editors or conversion strategy found]

Can anyone help me to save image in Mysql bank, using Spring boot?

My model Usuario :

@Entity
public class Usuario implements UserDetails, Serializable {

    private static final long serialVersionUID = 1L;
    private String foto;


    public String getFoto() {
        return foto;
    }

    public void setFoto(String foto) {
        this.foto = foto;
    }
}

The controller to save the form:

@PostMapping("/save")
public ModelAndView save(@Valid Usuario usuario, String senhaconf, @RequestParam("files[]") MultipartFile[] files, String senha, BindingResult result, RedirectAttributes attributes) {

    if (result.hasErrors() || !senha.equals(senhaconf)) {
        attributes.addFlashAttribute("mensagem", "[Verifique os campos!");
        System.out.println("--------erro ao salvar: " + usuario.getId());
        return cadastrarFornecedor(usuario);
    }
    String foto = files[0].getOriginalFilename();
    usuario.setFoto(foto);
    usuario.setSenha(new BCryptPasswordEncoder().encode(senha));
    service.save(usuario);
    attributes.addFlashAttribute("mensagem", "Evento cadastrado com sucesso!");
    return findAll();
}
    
asked by anonymous 11.06.2018 / 17:44

1 answer

0

Try changing @RequestParam("files[]") MultipartFile[] to @RequestParam("files") MultipartFile[]

    
11.06.2018 / 20:13