Error Converting Image to String to Save in Database

Asked

Viewed 210 times

2

I’m trying to save an image in my form at the bank, but I’m having difficulties in solving the error:

Field error in object 'usuario' on field 'foto': rejected value [org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@2218206a]; codes [typeMismatch.usuario.foto,typeMismatch.foto,typeMismatch.java.lang.String,typeMismatch]; arguments [org.springframework.context.support.Defaultmessagesourceresolvable: codes [usuario.foto,foto]; Arguments []; default message [foto]]; default message [Failed to Convert Property value of type 'org.springframework.web.multipart.support.Standardmultiparthttpservletrequest$Standardmultipartfile' to required type 'java.lang.String' for Property 'photo'; 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 'photo': no matching Editors or Conversion Strategy found]

Could someone help me save image in Mysql database 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();
}

1 answer

0


Try to change @RequestParam("files[]") MultipartFile[] for @RequestParam("files") MultipartFile[]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.