【问题标题】:PrimeFaces' Photocam : Showing Captured ImagePrimeFaces 的 Photocam : 显示捕获的图像
【发布时间】:2012-03-23 19:52:50
【问题描述】:

我试图用一个简单的变化来复制https://www.primefaces.org/showcase/ui/multimedia/photoCam.xhtml 的例子。

在示例中,每张拍摄的照片都被保存并显示。

但我只需要显示最新的,所以我删除了列表引用并稍微编辑了代码。

所以我只使用 graphicsImage 而不是 imageSwitch,并且在 onCapture 方法中我只是覆盖了之前的图像。

但这就是问题所在。

新图像已正确拍摄,我可以通过浏览其文件夹看到它,但不会在页面中更新。 似乎它仍然显示以前缓存的一个,因为如果它是第一个(在此之前没有文件),它就可以工作。

有什么建议吗?

编辑: 这里有一些代码。

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>  

我的 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 豆

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.");  
    }  
}  

我的豆

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);
    }
}

图像在我的 temp 文件夹中被捕获和更新,但它不会在页面中更改。 即使刷新也无济于事。 仅在服务器重新启动后才会更改!

编辑: 这是我的 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>

使用 Daniel 的解决方案,我应该能够浏览到 localhost:8080/Unilife5-war/uploads/avatar/cp99.png 以显示 /home/stefano/Documenti/UniLife /avatar/cp99.png ,还是我弄错了?

【问题讨论】:

  • 这似乎是正确的方法,但不知何故它不起作用!我已将 glassfih-web.xml 添加到第一篇文章中。

标签: jsf primefaces browser-cache


【解决方案1】:

对 glassfish 不太熟悉......但首先我会使用 BalusC 给出的最简单的例子

PrimeFace update after upload

<property name="alternatedocroot_1" value="from=/uploads/* dir=/var/webapp" />

And use http://localhost:8080/contextname/uploads/* to serve those uploaded images from by 

<h:graphicImage> the usual way. 

没有像

这样的其他内部文件夹

stefano/Documenti/UniLife/avatar/

你改变了你的 bean 代码吗?

File imageFile = new File(extContext.getRealPath("NEW//uploaded//temp") + "//" + utente.getUsername() + ".png");

【讨论】:

  • 再次感谢,但localhost:8080/contextname/uploads 给了我一个 404 错误。我想 contextname 是一个参数,但我已经尝试了所有组合但没有成功。我将关闭此问题并打开一个新问题,因为主题已更改!
  • 我认为 contextname 意味着您的网络应用程序名称 localhost:8080/yourWebAppName/uploads 不是吗?
  • 我也是这么想的,但它不起作用..我在 /var/webapp 中有一个文件 f.txt,localhost:8080/Unilife5-war/uploads/f.txt 显示 404 错误!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多