I have a web app and want to save images to my DB and then want retrieve the saved image back from the DB. I am able to upload the image to my web app by PrimeFaces fileUpload and save image properties to my DB. However, I cannot save the uploaeded image to some directory in my web app so that later I can retrieve its path and properties from DB and the image from the directory. I am getting the following error:
javax.el.ELException: /compositions/defineType.xhtml @122,56 fileUploadListener="#{typeBean.handleTypeImageUpload}": java.io.FileNotFoundException: C:\Users\mta\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\MyWebApp\webapp\images\uploadedImages\alarmPassive.gif
my file upload listener:
public void handleTypeImageUpload(FileUploadEvent event) throws IOException {
UploadedFile file = event.getFile();
Type newTypeProp = new Type();
newTypeProp .setTypeName("someName");
newTypeProp .setIconFileName(imageFile.getFileName());
newTypeProp .setIcon(imageFile.getContents());
databaseDao.saveType(newTypeProp );
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
String prefix = FilenameUtils.getBaseName(file.getFileName());
String suffix = FilenameUtils.getExtension(file.getFileName());
File fileToDirectory = File.createTempFile(prefix + "-", "." + suffix, new File("C:\\temp"));
InputStream is = event.getFile().getInputstream();
OutputStream out = new FileOutputStream(fileToDirectory);
byte buf[] = new byte[1024];
int len;
while ((len = is.read(buf)) > 0)
out.write(buf, 0, len);
is.close();
out.close();
}
my type backing bean that is saved to DB:
public class Type {
private Integer typeId;
private String typeName;
private byte[] icon;
private String iconFileName;
// getters & setters
...
}
xhtml file uploading:
<p:fileUpload style="font-size:11px;" fileUploadListener="#{typeBean.handleTypeImageUpload}"
mode="advanced" dragDropSupport="true"
sizeLimit="100000" fileLimit="3" update="messages"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
label="#{general.select}" uploadLabel="#{general.upload}"
cancelLabel="#{general.cancel}">
</p:fileUpload>
Now, how can I save the image file to some directory in my web app so that later I can get it? Any help would greatly be appreciated.