0

我正在做一些项目,我需要实现图像上传和查看图像功能,但我在这里遇到了一些问题。

我无法显示图像,请您帮帮我。

我正在使用来自 primefaces 的 p:fileload,并且我将所有图像存储在 jboss 服务器的 bin 文件夹中(E:\jboss-as-7.1.1.Final\bin\BrandImages\logo.gif)。并将该路径存储在 MYSQL DB 中,直到现在它工作正常。但是当我试图通过调用 getBrandDetails() 来显示图像时,它无法显示。你能告诉我我该如何解决这个问题吗???

在 web.xml 中

在 web.xml 我添加了过滤器和 mime ......

Xhtml

<h:form enctype="multipart/form-data">
   <h:panelGrid id="brandDetails_Panel" columns="2" columnClasses="plabel, pvalue" styleClass="ui-panelgrid">
        <h:outputLabel for="brand_img_url" value="Brand Image" />
    <p:fileUpload id="brand_img_url" fileUploadListener="#{brandBean.handleFileUpload}"  
        mode="advanced"  sizeLimit="100000" allowTypes="/(\.\/)(gif|jpe?g|png)$/"/>

        <h:outputLabel value="" />
    <p:graphicImage value="#{brandBean.brand_image}" alt="The image could not be found."/>
    </h:panelGrid>
<f:facet name="footer" >
    <center>
        <p:commandButton id="add" value="Add"  disabled="#{brandBean.disable_addBut}" actionListener="#{brandBean.addBrandDetails}"/>
        <p:commandButton id="view" value="View" disabled="#{brandBean.disable_viewBut}"  action="#{brandBean.getBrandDetails}"/>         
    </center>       
</f:facet>
</h:form>

在 addBrandDetails() 中,handleFileUpload() 为

public void handleFileUpload(FileUploadEvent event) 
{
String t1=".\\BrandImages\\"+event.getFile().getFileName();
System.out.println("The final path is :"+t1);

try{
    File f =new File(t1);

    FileOutputStream fileOutputStream = new FileOutputStream(f);

    byte[] buffer = new byte[1024];

        int bulk;
        InputStream inputStream = event.getFile().getInputstream();
        while (true) {
          bulk = inputStream.read(buffer);
          if (bulk < 0) {
                 break;
                 }
          fileOutputStream.write(buffer, 0, bulk);
          fileOutputStream.flush();
          }
          fileOutputStream.close();
          inputStream.close();
}catch(Exception e)
{
    System.out.println("Exception :"+e);
}   

this.brand_image=t1;
}               

在 getBrandDetails() 中,我设置为“this.setBrand_image(b.getBrand_img_url());”

当我正在打印时,它正在打印路径 E:\jboss-as-7.1.1.Final\bin\BrandImages\logo.gif

但它无法显示图像。

请帮帮我....

4

1 回答 1

0

首先,您的 bean 必须具有会话的范围。一个好方法是只制作一个 bean 来获取请求的图像。

其次,您的变量“brand_image”需要是 DefaultStreamedContent 的对象。因为您使用的是primefaces。

brand_image = new DefaultStreamedContent(input, "image/jpeg");
于 2013-03-01T17:04:40.163 回答