I'm trying to replicate the example of https://www.primefaces.org/showcase/ui/multimedia/photoCam.xhtml with a simple variation.
In the example, every taken picture is saved and showed.
But I just need to show the latest one, so I've removed the list references and edited the code a little bit.
So I'm using just the graphicsImage instead of the imageSwitch, and, in the onCapture method I just overwrite the previous image.
But here's the problem.
The new image is correctly taken, I can see it by browsing to its folder, but it won't be updated in the page. It seems that it still shows the previous cached one, because it works if it's the first one (no file before this take).
Any suggestion?
EDIT: Here some code.
PrimeFaces JSF
<h:form>
<h:panelGrid columns="3">
<p:photoCam widgetVar="pc" listener="#{photoCamBean.oncapture}" update="photos"/>
<p:commandButton type="button" value="Capture" onclick="pc.capture()"/>
<p:imageSwitch effect="zoom" id="photos">
<ui:repeat value="#{photoCamBean.photos}" var="photo">
<p:graphicImage value="/photocam/#{photo}.png" />
</ui:repeat>
</p:imageSwitch>
</h:panelGrid>
MY JSF
<p:photoCam widgetVar="pc" listener="#{registerBean2.onCapture}" update="photo"/>
<p:commandButton type="button" value="Capture" onclick="pc.capture()" update="photoPanel"/>
<p:panel id="photoPanel">
<p:graphicImage value="./uploaded/temp/#{registerBean2.utente.username}.png" id="photo">
<p:effect type="pulsate" event="load" delay="500">
<f:param name="mode" value="'show'" />
</p:effect>
</p:graphicImage>
</p:panel>
PRIMEFACES BEAN
public void oncapture(CaptureEvent captureEvent) {
String photo = getRandomImageName();
this.photos.add(0,photo);
byte[] data = captureEvent.getData();
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String newFileName = servletContext.getRealPath("") + File.separator + "photocam" + File.separator + photo + ".png";
FileImageOutputStream imageOutput;
try {
imageOutput = new FileImageOutputStream(new File(newFileName));
imageOutput.write(data, 0, data.length);
imageOutput.close();
}
catch(Exception e) {
throw new FacesException("Error in writing captured image.");
}
}
MY BEAN
public void onCapture(CaptureEvent captureEvent) {
makeAvatar(captureEvent.getData());
}
private void makeAvatar(byte[] imageData) {
FileImageOutputStream imageOutput;
ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
File imageFile = new File(extContext.getRealPath("NEW//uploaded//temp") + "//" + utente.getUsername() + ".png");
try {
if (imageFile.exists()) {
imageFile.delete();
}
imageFile.createNewFile();
imageOutput = new FileImageOutputStream(imageFile);
imageOutput.write(imageData, 0, imageData.length);
imageOutput.close();
FacesMessage msg = new FacesMessage("Immagine caricata correttamente.");
FacesContext.getCurrentInstance().addMessage(null, msg);
} catch (Exception e) {
FacesMessage msg = new FacesMessage("Errore nel caricamento dell'immagine!");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
The image is captured and updated in my temp folder, but it won't change in the page. Even a refresh won't help me. It changes only after a server restart !
EDIT : Here's my glassfish-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
<class-loader delegate="true"/>
<jsp-config>
<property name="keepgenerated" value="true">
<description>Keep a copy of the generated servlet class' java code.</description>
</property>
<property name="alternatedocroot_1" value="from=/uploads/* dir=/home/stefano/Documenti/UniLife" />
</jsp-config>
</glassfish-web-app>
Using Daniel's solution, I should have been able to browse to localhost:8080/Unilife5-war/uploads/avatar/cp99.png to show /home/stefano/Documenti/UniLife/avatar/cp99.png , or I'm getting wrong?