0

i have this errore since few weeks now... I-m trying to upload an image from my html template to BD... i have this error:

2023-07-04 12:42:00.503 WARN 18708 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errorsField error in object 'equipe' on field 'drapeau': rejected value [org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@62690d90]; codes [typeMismatch.equipe.drapeau,typeMismatch.drapeau,typeMismatch.[B,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [equipe.drapeau,drapeau]; arguments []; default message [drapeau]]; default message [Failed to convert property value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'byte[]' for property 'drapeau'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'byte' for property 'drapeau[0]': PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned inappropriate value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile']]

Equipe model


@Data
@Entity
@Table(name="equipe")
@Getter
@Setter



@AllArgsConstructor
@NoArgsConstructor

public class Equipe implements Serializable {


    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)


    private Long idEquipe;


    private String pays;

    private Long nbr_points;


    @Lob
    private byte[] drapeau;

    @OneToMany(mappedBy="equipe")
    private List<Joueur> joueur = new ArrayList<>();



    public String getPays() {
        return pays;
    }

    public void setPays(String pays) {
        this.pays = pays;
    }

    public Long getNbr_points() {
        return nbr_points;
    }

    public void setNbr_points(Long nbr_points) {
        this.nbr_points = nbr_points;
    }


    public byte[] getDrapeau() {
        return drapeau;
    }

    public void setDrapeau(byte[] drapeau) {
        this.drapeau = drapeau;
    }

    public List<Joueur> getJoueur() {
        return joueur;
    }

    public void setJoueur(List<Joueur> joueur) {
        this.joueur = joueur;
    }
    
    

    public Equipe(String pays) {
        this.pays = pays;
    }

    public Equipe(String pays, Long nbr_points, byte[] drapeau) {
        this.pays = pays;
        this.nbr_points = nbr_points;
        this.drapeau = drapeau;
    }

    public Equipe(String pays, Long nbr_points) {
        this.pays = pays;
        this.nbr_points = nbr_points;
    }

    @Override
    public String toString() {
        return "Equipe{" +
                "idEquipe=" + idEquipe +
                ", pays='" + pays + '\'' +
                ", nbr_points=" + nbr_points +
                ", drapeau=" + drapeau +

                '}';
    }
}

Equipe Service

     public void addEquipe( String pays, Long nbr_points,MultipartFile file)


    {

        Equipe equipe = new Equipe();

        String fileName = StringUtils.cleanPath(file.getOriginalFilename());
        if(fileName.contains(".."))
        {
            System.out.println("not a a valid file");
        }
        try {
            byte [] content = file.getBytes();
            equipe.setDrapeau(content);

        } catch (IOException e) {
            e.printStackTrace();
        }
       equipe.setPays(pays);

        equipe.setNbr_points(nbr_points);





        equipeRepository.save(equipe);


    }


Equipe controller

   public String equipeFormAdd(Model model)
   {
       model.addAttribute(new Equipe());

       return"equipe/add";
   }

  @PostMapping("/equipe/add")
   public String equipeSubmitAdd(@Valid @ModelAttribute("equipe")Equipe equipe, @RequestParam("drapeau") MultipartFile file,@RequestParam("pays") String pays,@RequestParam("nbr_points") Long nbr_points, BindingResult result, ModelMap model) throws IOException {

        if (result.hasErrors()) {
           return "equipe/add";
       }






        equipeService.addEquipe(pays,nbr_points,file);
       model.addAttribute("equipe", equipe);
       //model.addAttribute("image", "");
       return  "redirect:/equipe/add";
   }

html

<!DOCTYPE html>
<html   lang="fr"
        xmlns="http://www.w3.org/1999/xhtml"

        xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap core CSS -->
    <link href="../static/css/bootstrap.css" rel="stylesheet" th:href="@{/css/bootstrap.css}"/>
    <link href="../static/js/bootstrap.js" rel="stylesheet" type="text/javascript" th:href="@{/js/bootstrap.js}"/>
    <link href="../static/css/index/index/index.css" th:href="@{/css/index/index.css}" rel="stylesheet" />
    <title>Fiche d'un artiste</title>
    <!--colore errore messaggi-->
    <style>
        .error{
            color:red;
        }
    </style>
</head>


<body>


<div class="container">

        <form  th:action="@{'/equipe/add'}" th:object="${equipe}" method="post" class="form"  enctype="multipart/form-data"> <!-- th:object ci serve per fare collegamento con il model -->
            <!-- <h1 th:text="|${artist.firstname} ${artist.lastname}|">ARMEN sulu</h1> -->
            <!--<div class="form-group">
                <strong> <label for="id">id</label></strong>
                <input class="form-control" type="number"  th:field="*{id}" id="id" placeholder="id">
                <div class="alert alert-warning" th:if="${#fields.hasErrors('id')}" th:errors="*{id}"></div>
            </div>-->

            <div class="form-group">
                <strong> <label for="pays">Pays</label></strong>
                <input class="form-control" type="text"  th:field="*{pays}" id="pays" placeholder="pays">
                <div class="alert alert-warning" th:if="${#fields.hasErrors('pays')}"  th:errors="*{pays}" th:errorclass="error"></div>
            </div>

            <div class="form-group">
                <strong><label for="nbr_points">Nbr Points</label></strong>
                <!-- th field permette di recuperare il nome selezionato -->
                <input class="form-control" type="text" th:field="*{nbr_points}"  id="nbr_points" placeholder="nbr_points">
                <!-- place holder suggerimento allinterno del box -->
                <div class="alert alert-warning" th:if="${#fields.hasErrors('nbr_points')}" th:errors="*{nbr_points}" th:errorclass="error"></div>
            </div>



            <div class="form-group">Drapeau</label></strong>
                <!-- th field permette di recuperare il nome selezionato -->
                <input class="form-control" type="file"  name="drapeau" th:field="*{drapeau}"  id="drapeau" placeholder="drapeau"  />
                <!-- place holder suggerimento allinterno del box -->
                <div class="alert alert-warning" th:if="${#fields.hasErrors('drapeau')}" th:errors="*{drapeau}" th:errorclass="error" ></div>
            </div>


            <!-- th:errorclass="error" serve per indicare lo style del errore, il colore che ho definito sopra -->
            <br/>

            <input class="btn btn-primary" type="submit" value="Submit" /><br/><br/>

            <!-- class="btn btn-primary" serve per il design del bottone -->


            <!-- <div><a th:href="@{/artists/{id}}">annuler</a></div> -->
            <nav><a th:href="@{/}">Annuler</a></nav>

        </form>

    </div>
</div>

</body>
</html>

properties

spring.servlet.multipart.enabled=true

# Threshold after which files are written to disk.

spring.servlet.multipart.file-size-threshold=2KB
# Max file size.
spring.servlet.multipart.max-file-size=200MB
# Max Request Size
spring.servlet.multipart.max-request-size=215MB

0 Answers0