primeFaces:fileUpload to byte [] [英] primeFaces : fileUpload to byte[]

查看:163
本文介绍了primeFaces:fileUpload to byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将< p:fileUpload> 的上传图像作为 byte [] 它由JPA在DB中。但是我面临一个问题,我甚至不知道我是否以正确的方式编码。



这是实体:

  @Entity 
class Object {
@Lob
@Column(name =image)
私人字节[]图像;
// ...
}

托管bean:

  @ManagedBean 
class MyBean {

private Object ob = new Object();

@EJB
private ObjectFacadeLocal of;

public void handleFileUpload(FileUploadEvent event){
byte [] content = event.getFile()。getContents();
ob.setImage(content);


public String submit(){
// ...
of.create(ob);
returnanotherpage
}

ObjectFacade persists Object 在数据库中。

这是名为<$ c $的JSF页面c> form.xhtml

 < h:form id =upienctype =多部分/格式数据> 
allowTypes =*。jpg; *。png; *。gif;描述= 影像/>
< h:commandButton value =Submitaction =#{MyBean.create()}>
< / h:commandButton>
< / h:表格>

首先,我在 form.xhtml ,它找不到 handleFileUpload 方法,但是我仍然可以运行它。当我按提交按钮,然后没有任何反应,页面刷新。如果我删除了 enctype 属性,那么这个对象会被保存下来,但是不会与图像一起被保留下来。

有什么想法吗? / b>




更新
$ b 是我的 web.xml 文件:

 <?xml version = 1.0encoding =UTF-8?> 
< web-app version =3.0xmlns =http://java.sun.com/xml/ns/javaeexmlns:xsi =http://www.w3.org/2001/ XMLSchema-instancexsi:schemaLocation =http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee /web-app_3_0.xsd\">
< context-param>
< param-name> javax.faces.PROJECT_STAGE< / param-name>
<参数值>开发< /参数值>
< / context-param>
< servlet>
< servlet-name> Faces Servlet< / servlet-name>
< servlet-class> javax.faces.webapp.FacesServlet< / servlet-class>
<加载启动> 1< /加载启动>
< / servlet>
< servlet-mapping>
< servlet-name> Faces Servlet< / servlet-name>
< url-pattern> / faces / *< / url-pattern>
< / servlet-mapping>
< session-config>
< session-timeout>
30
< / session-timeout>
< / session-config>
< welcome-file-list>
< welcome-file> faces / blog.xhtml< / welcome-file>
< / welcome-file-list>
< filter>
< filter-name> PrimeFaces FileUpload Filter< / filter-name>
< filter-class> org.primefaces.webapp.filter.FileUploadFilter< / filter-class>
< / filter>
< filter-mapping>
< filter-name> PrimeFaces FileUpload Filter< / filter-name>
< servlet-name> Faces Servlet< / servlet-name>
< / filter-mapping>
< / web-app>


解决方案

您忘记阅读 PrimeFaces用户指南。以下是< p:fileUpload> 章节的摘录:


开始使用FileUpload



首先要做的是配置解析多部分请求的文件上载过滤器。 FileUpload过滤器应映射到Faces Servlet。

< filter>
< filter-name> PrimeFaces FileUpload Filter< / filter-name>
< filter-class> org.primefaces.webapp.filter.FileUploadFilter< / filter-class>
< / filter>
< filter-mapping>
< filter-name> PrimeFaces FileUpload Filter< / filter-name>
< servlet-name> Faces Servlet< / servlet-name>
< / filter-mapping>


没有这个过滤器,< h:form enctype =multipart / form-data> 可以被调用,也不会被正确解析。

至于无法找到该方法的警告,这与您正在使用的IDE假装比它更聪明有关。在该IDE中禁用EL验证,以避免这些令人困惑的警告,并查看是否无法升级IDE。


I am trying to get the uploaded image of <p:fileUpload> as byte[] and persist it in DB by JPA. But I'm facing a problem and I'm not even sure if I'm coding it the right way.

This is the entity:

@Entity
class Object { 
  @Lob
  @Column(name = "image")
  private byte[] image;
  //... 
}

Managed bean:

@ManagedBean
class MyBean {

   private Object ob = new Object();

   @EJB 
   private ObjectFacadeLocal of;

   public void handleFileUpload(FileUploadEvent event) {
      byte[] content = event.getFile().getContents();
      ob.setImage(content);
   }

   public String submit() {
      //...
      of.create(ob);
      return "anotherpage"
   }

The ObjectFacade persists Object in database.

This is the JSF page called form.xhtml:

<h:form id = "upi" enctype = "multipart/form-data"> 
    <p:fileUpload fileUploadListener="#{MyBean.handleFileUpload}"   
          allowTypes="*.jpg;*.png;*.gif;" description="Images"/>  
    <h:commandButton value = "Submit" action = "#{MyBean.create()}">
    </h:commandButton>
</h:form>

First of all, I'm getting a warning in form.xhtml that it cannot find the handleFileUpload method, but I can still run it. When I press the submit button, then nothing happens, the page just refreshes. If I remove the enctype attribute then the object gets persisted, but not with the image.

Any ideas?


Update:

This is my web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee /web-app_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/blog.xhtml</welcome-file>
    </welcome-file-list>
    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
</web-app>

解决方案

You forgot to read the PrimeFaces User Guide. Here's an extract of <p:fileUpload> chapter:

Getting started with FileUpload

First thing to do is to configure the fileupload filter which parses the multipart request. FileUpload filter should map to Faces Servlet.

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

Without that filter, no one action method inside a <h:form enctype="multipart/form-data"> can be invoked nor will the submitted data be properly parsed.

As to the warning that the method cannot be found, this is related to the IDE which you're using which is pretending to be smarter than it is. Disable EL validation in that IDE to avoid those confusing warnings and look if you can't upgrade the IDE.

这篇关于primeFaces:fileUpload to byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆