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
| public void handleFileUpload(FileUploadEvent event) {
InputStream is;
String fileName = event.getFile().getFileName();
// Le dossier "images" DOIT exister dans le dossier "web" de votre application
// Il peut avoir un autre nom mais il DOIT exister à cet endroit précis et nulle part ailleurs
String relativeDestination = "//images//";
//String absoluteDestination = "C:\\testUpload\\destination\\";
try {
//File tempFile = new File(absoluteDestination + fileName);
File tempFile = new File(FacesContext.getCurrentInstance().getExternalContext().getRealPath(relativeDestination + fileName));
is = event.getFile().getInputstream();
OutputStream os = new FileOutputStream(tempFile);
byte buf[] = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
is.close();
System.out.print("fileName" + fileName);//test
} catch (IOException ex) {
ex.printStackTrace();
}
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
} |
Partager