[PrimeFaces] Enregistrement d'image sur MySql avec JPA et Primefaces
Bonjour tout le monde, j'ai essayé d'enregistrer une image sur ma base de données mais je n'arrive pas.
En effet j'utilise MYSQL comme base de données, JPA comme la ^persistance des données et primeface pour la vue.
j'ai vu que sur primeface, il existe ce tag <p:fileUpload> pour le chargement du fichier. j'ai donc essayer ce ci mais rien ne marche:
EntityBean
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| public class Fichier implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "identifiant")
private Integer identifiant;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 30)
@Column(name = "nom")
private String nom;
@Lob
@Column(name = "CONTENT")
private byte[] content;
public Fichier() {
}
public Fichier(Integer identifiant) {
this.identifiant = identifiant;
}
public Fichier(Integer identifiant, String nom) {
this.identifiant = identifiant;
this.nom = nom;
}
public Integer getIdentifiant() {
return identifiant;
}
public void setIdentifiant(Integer identifiant) {
this.identifiant = identifiant;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
@Override
public int hashCode() {
int hash = 0;
hash += (identifiant != null ? identifiant.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Fichier)) {
return false;
}
Fichier other = (Fichier) object;
if ((this.identifiant == null && other.identifiant != null) || (this.identifiant != null && !this.identifiant.equals(other.identifiant))) {
return false;
}
return true;
}
@Override
public String toString() {
return "classe.entities.Fichier[ identifiant=" + identifiant + " ]";
} |
Méthode de récupération
Code:
1 2 3 4
| public void fileUpload(FileUploadEvent event) throws IOException {
byte[] content = event.getFile().getContents();
//byte[] content=IOUtils.toByteArray(event.getFile().getInputstream());
} |
fichier xhtml
Code:
1 2 3
| <p:inputText id="file" value="#{fichierController.selected.content}">
<p:fileUpload fileUploadListener="#{fichierController.fileUpload}" mode="simple" allowTypes="*.jpg;*.png;*.gif" />
</p:inputText> |
Merci de votre compréhension