primeFaces : 文件上传到字节[] [英] primeFaces : fileUpload to byte[]

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

问题描述

我试图将上传的 <p:fileUpload> 图像作为 byte[] 并通过 JPA 将其保存在 DB 中.但是我遇到了一个问题,我什至不确定我的编码方式是否正确.

这是实体:

@Entity类对象{@高球@Column(name = "图片")私有字节[]图像;//...}

托管 bean:

@ManagedBean类 MyBean {私有对象 ob = 新对象();@EJB私有 ObjectFacadeLocal 的;public void handleFileUpload(FileUploadEvent 事件) {byte[] content = event.getFile().getContents();ob.setImage(内容);}公共字符串提交(){//...of.create(ob);返回另一页"}

ObjectFacade 在数据库中持久化Object.

这是名为 form.xhtml 的 JSF 页面:

<p:fileUpload fileUploadListener="#{MyBean.handleFileUpload}"allowTypes="*.jpg;*.png;*.gif;"描述=图像"/><h:commandButton value = "Submit" action = "#{MyBean.create()}"></h:commandButton></h:form>

首先,我在 form.xhtml 中收到一个警告,提示它找不到 handleFileUpload 方法,但我仍然可以运行它.当我按下提交按钮时,没有任何反应,页面只是刷新.如果我删除 enctype 属性,则对象将被持久化,但不会与图像一起存储.

有什么想法吗?

<小时>

更新:

这是我的 web.xml 文件:

<上下文参数><param-name>javax.faces.PROJECT_STAGE</param-name><param-value>开发</param-value></context-param><小服务程序><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><会话配置><会话超时>30</会话超时></session-config><欢迎文件列表><welcome-file>faces/blog.xhtml</welcome-file></welcome-file-list><过滤器><filter-name>PrimeFaces 文件上传过滤器</filter-name><filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class></过滤器><过滤器映射><filter-name>PrimeFaces 文件上传过滤器</filter-name><servlet-name>Faces Servlet</servlet-name></过滤器映射></web-app>

解决方案

您忘记阅读 PrimeFaces 用户指南.这是章节的摘录:

<块引用>

开始使用文件上传

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

<filter-name>PrimeFaces 文件上传过滤器</filter-name><filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class></过滤器><过滤器映射><filter-name>PrimeFaces 文件上传过滤器</filter-name><servlet-name>Faces Servlet</servlet-name></过滤器映射>

如果没有那个过滤器,<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 : 文件上传到字节[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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