I would like to upload an image and store it on the server, and later to show it with h:graphicImage? I would like to store it in \"resources/images\" of the app. I am using
Wasn't able to get it working with Path#write in glassfish, so I used Path#getInputStream as follows:
public void upload(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
String filename = getFilename(uploadedFile);
File file = new File("/var/webapp/images/"+filename);
bis = new BufferedInputStream(uploadedFile.getInputStream());
FileOutputStream fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
int x;
while((x = bis.read())!= -1){
bos.write(x);
}
} catch (IOException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
finally{
try {
bos.flush();
bos.close();
bis.close();
} catch (IOException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private static String getFilename(Part part) {
for (String cd : part.getHeader("content-disposition").split(";")) {
if (cd.trim().startsWith("filename")) {
String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
}
}
return null;
}