Bonjour , je suis entrain de faire un formulaire de saisie, en fait l'utilisateur va uploder son CV (fichier pdf) , pour faire la distinction entre les fichiers, je dois modifier le nom du cv par ID.pdf, le problème c'est que après avoir utiliser le composant <p:fileupload> de primefaces , le fichier se stocke imédiatement dans le disque sans pour autant , cliquer sur submit, donc je peux pas le modifier par getID !!

Voici le JSFManagedBean :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
@ManagedBean(name="candsBean")
@ViewScoped
public class Candsean implements Serializable {
 
    private Recs recs;
    private List<Recs> recss;
    private List<String> emails = new ArrayList<String>();
    private List<String> cins = new ArrayList<String>();
    private String destination="C:\\temp\\candidaturespontannee\\";
    private String pdfPath;
    private boolean displayed = false;
    private String nomPDF;
    @ManagedProperty("#{fileUploadController}")
    FileUploadController fileUploadController;
 
    public Candsean() {
        recs = new Recs();
    }
 
     @PostConstruct
    public void init(){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Query q = session.createQuery("FROM Recs");
        List<Recs> list = q.list();
        for (Recs recs:list){
            emails.add(recs.getEmail());
            cins.add(recs.getCin());
        }
 
    }
 
      public void preparerInsertion() {
        recs = new Recs();
 
    }
 
        public String insertion() {
        CandsDao candidatspontanneeDao = new CandsDaoImpl();
        if(emails.contains(recs.getEmail()) ){
            FacesContext ctx = FacesContext.getCurrentInstance();
            ctx.addMessage(null, new FacesMessage("Vous etes déjà inscrit"));
            return null;
 
        }
 
        if (cins.contains(recs.getCin())){
            FacesContext ctx = FacesContext.getCurrentInstance();
            ctx.addMessage(null, new FacesMessage("Vous etes déjà inscrit"));
            return null;   
        }   
        nomPDF=""+recs.getCin()+".pdf";
        candidatspontanneeDao.inserer(recs);
        return "bienvenue";
    }
 
    public Recs getRecs() {
        return recs;
    }
 
    public void setRecs(Recs recs) {
        this.recs = recs;
    }
 
    public List<Recs> getRecss() {
        return recss;
    }
 
    public void setRecss(List<Recs> recss) {
        this.recss = recss;
    }
 
    public List<String> getEmails() {
        return emails;
    }
 
    public void setEmails(List<String> emails) {
        this.emails = emails;
    }
 
    public List<String> getCins() {
        return cins;
    }
 
    public void setCins(List<String> cins) {
        this.cins = cins;
    }
 
          public List<Recs> getCandidatspontannees() {
        CandsDao candidatspontanneeDao = new CandsDaoImpl();
        recss = candidatspontanneeDao.chercherTous();
        return recss;
    }
 
    public FileUploadController getFileUploadController() {
        return fileUploadController;
    }
 
    public void setFileUploadController(FileUploadController fileUploadController) {
        this.fileUploadController = fileUploadController;
    }
 
 
 public void upload(FileUploadEvent event) { 
 
       // Do what you want with the file 
       try {
           String fileName = event.getFile().getFileName();
           //fileName + ".pdf"
           copyFile(fileName, event.getFile().getInputstream());
 
           FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");  
           FacesContext.getCurrentInstance().addMessage(null, msg);
           displayed=!displayed;
 
       }catch (IOException e) {
           e.printStackTrace();
       }
 
   }
 
    public void copyFile(String fileName, InputStream in) throws FileNotFoundException {
        try {
            // write the inputStream to a FileOutputStream
            OutputStream out = new FileOutputStream(new File(destination + fileName));
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            in.close();
            out.flush();
            out.close();
            System.out.println("New file created!");
        }catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
 
    public String getDestination() {
        return destination;
    }
 
    public void setDestination(String destination) {
        this.destination = destination;
    }
 
    public String getPdfPath() {
        return pdfPath;
    }
 
    public void setPdfPath(String pdfPath) {
        this.pdfPath = pdfPath;
    }
 
    public boolean isDisplayed() {
        return displayed;
    }
 
    public void setDisplayed(boolean displayed) {
        this.displayed = displayed;
    }
 
    public String getNomPDF() {
        return nomPDF;
    }
 
    public void setNomPDF(String nomPDF) {
        this.nomPDF = nomPDF;
    }

et dans le JSF Page :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
        <h:outputLabel for="cv" value="Ajouter CV : *" />
         <p:outputPanel id="file"   >
             <p:fileUpload   rendered="#{!candsBean.displayed}"  style="font-size: 100% !important;"  fileUploadListener="#{candsBean.upload}"    update="file,messages" sizeLimit="10000000" allowTypes="/(\.|\/)(pdf)$/" auto="true"  showButtons="false" label="Choisissez un fichier" /> 
         <p:growl  id="messages" showDetail="true"/> 
         </p:outputPanel>