PrimeFaces p:fileUpload 不调用方法

Posted

技术标签:

【中文标题】PrimeFaces p:fileUpload 不调用方法【英文标题】:PrimeFaces p:fileUpload does not invoke method 【发布时间】:2012-12-10 11:20:29 【问题描述】:

我正在尝试使用 PrimeFaces <p:fileUpload>。我已经设置好了,与the showcase example 非常相似,但它不起作用。我从来没有收到成功的消息。我没有得到任何错误或任何东西。

这是我的web.xml

<?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>
    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
        <init-param>
            <param-name>thresholdSize</param-name>
            <param-value>51200</param-value>
        </init-param>
        <init-param>
            <param-name>uploadDirectory</param-name>
            <param-value>C:\etc</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

这是视图:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">

    <h:head></h:head>

    <h:form enctype="multipart/form-data">
        <p:fileUpload fileUploadListener="#fileUploadController.handleFileUpload"
                      mode="advanced" 
                      update="messages"
                      sizeLimit="100000" 
                      allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>

        <p:growl id="messages" showDetail="true"/>
    </h:form>

</html>

    </h:form>
  </h:body>

这是豆子:

package fileUpload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;

@ManagedBean(name = "fileUploadController")
public class fileUploadController 

    private String destination = "c:\test";

    public void upload(FileUploadEvent event) 
        FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        // Do what you want with the file        
        try 
            copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
         catch (IOException e) 
            e.printStackTrace();
        

    

    public void copyFile(String fileName, InputStream in) 
        try 


            // write the inputStream to a FileOutputStream
            OutputStream out = new FileOutputStream(new File(destination + fileName));

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = in.read(bytes)) != -1) 
                out.write(bytes, 0, read);
            

            in.close();
            out.flush();
            out.close();

            System.out.println("New file created!");
         catch (IOException e) 
            System.out.println(e.getMessage());
        
    

我错过了什么?

WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /FileUpload, because request parameters have already been read, or ServletRequest.getReader() has already been called
WARNING: /index.xhtml @15,63 fileUploadListener="#fileUploadController.handleFileUpload(event)": Method handleFileUpload not found
javax.el.MethodNotFoundException: /index.xhtml @15,63 fileUploadListener="#fileUploadController.handleFileUpload(event)": Method handleFileUpload not found
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:109)
    at org.primefaces.component.fileupload.FileUpload.broadcast(FileUpload.java:279)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:759)
    at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:935)
    at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:77)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722).

【问题讨论】:

请检查这是否还没有回答您的问题:***.com/questions/8875818/… 您好,我已经尝试按照其中的设置进行操作,但我仍然无法上传图像,它会让我上传它,但没有成功的消息或任何东西,所以我认为它没有上传它 好的,您在 webapp 的运行时类路径中也有 Commons-FileUpload 和 Commons-IO 吗?顺便说一句,你的观点很奇怪。表格不在正文中。这是对问题的粗心准备还是反映了真实的代码? 我已将 Commons-FileUpload 和 Commons-IO 添加到 Web-INF/lib 中,除了将它们包含在此处之外,我还没有做任何其他事情,我读到这应该足够了,我应该做些什么还有这些吗?我正在使用 netbeans 和 glassfish 查看日志,有没有报错? 【参考方案1】:

例外情况不言自明。

javax.el.MethodNotFoundException: /index.xhtml @15,63 fileUploadListener="#fileUploadController.handleFileUpload(event)": 找不到方法句柄文件上传

这个方法确实不存在。而是这个:

public void upload(FileUploadEvent event) 

使用#fileUploadController.upload 或将方法重命名为handleFileUpload

【讨论】:

非常感谢:),上传的内容保存在哪里?正如我确实尝试将它们保存到 c:\test 但这里什么都没有 好的,c:/test,但是现在当它试图保存在这里时,它说访问被拒绝,我可以给它访问权限吗?

以上是关于PrimeFaces p:fileUpload 不调用方法的主要内容,如果未能解决你的问题,请参考以下文章

Primefaces FileUpload 与 PrettyFaces 和 JSF 2.2.3

<p:fileUpload> 总是给我空内容[重复]

p:fileUpload 未在支持 bean 中设置上传的文件 [重复]

如何在简单模式和 ajax="true" 中使用 <p:fileUpload>?

p:fileupload图像预览不起作用

如何在 JSF 中保存上传的文件