JSF 2.2-文件上传返回java.lang.NullPointerException [英] JSF 2.2 - File upload return java.lang.NullPointerException

查看:171
本文介绍了JSF 2.2-文件上传返回java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<h:inputFile/>返回NullPointerException错误.我知道已经有很多关于此异常以及使用JSF上传文件的信息,但是我已经读完了所有内容,但未能成功诊断出该问题.

<h:inputFile/> return a NullPointerException error. I know there is already a lot of stuff about this exception and uploading files with JSF but I've read it all without any success to diagnose this problem.

我正在使用带有GlassFish 4.1.1的JSF 2.2,而没有PrimeFaces或Tomahawk,因为您可以使用JSF 2.2处理文件上传.正如我所读.

I am using JSF 2.2 with GlassFish 4.1.1, without PrimeFaces or Tomahawk since you can handle the file upload with JSF 2.2. as I read.

目标只是将图片上传到远程文件系统上,然后将路径复制到DB,以便稍后通过数据表进行检索.

The goal is just to upload a picture on the remote filesystem, and copy the path to the DB to retrieve it later through a datatable.

我已经检查过了:

1-我的表单具有enctype="multipart/form-data"属性.

1 - My form has the enctype="multipart/form-data" attribute.

2-我的表单虽然是<ui:composition/>的一部分,但并未嵌套在其他表单中.

2 - My form is not nested in another form though it is part of <ui:composition/>.

3-我已将所有适当的过滤器设置为处理multipart/form-data类型:

3 - I have set all proper filter to process multipart/form-data type :

<filter>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

4-当我在create()方法中删除<h:inputFile/>标记及其部分时,发送数据毫无问题.

4 - When I remove the <h:inputFile/> tag and its part in the create() method, sending data works without any problem.

5-我试图将ManagedBean分别声明为@ApplicationScoped@ViewScoped@RequestScoped.

5 - I tried to state the ManagedBean as @ApplicationScoped, @ViewScoped and @RequestScoped without any change.

6-我试图使用抽象方法将initialize Part属性放入ManagedBean中,但没有成功.

6 - I tried to put the initialize Part attribute in the ManagedBean with abstract methods without any success.

这是我的表格:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:f="http://xmlns.jcp.org/jsf/core"
                xmlns:h="http://xmlns.jcp.org/jsf/html"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
                xmlns:t="http://myfaces.apache.org/tomahawk">
    <h:form enctype="multipart/form-data">
        <h:outputLabel value="Marque : " />
        <h:inputText value="#{carController.car.brand}" />
        <br/>
        few same inputText
        <h:outputLabel value="Photo : " />
        <h:inputFile value="#{carController.picture}" required="true" requiredMessage="No file selected."/>
        <br/>
        <h:commandButton value="Valider" action="#{carController.create}" />
    </h:form>
</ui:composition>


这是我的ManagedBean,我已发表评论,因为这是处理我尝试过的图像上传的其他方式:

@Named(value = "carController")
@SessionScoped
public class CarController implements Serializable {
    private static final long serialVersionUID = 1L;
    @EJB
    private CarFacade carFacade;

    private Car car;
    private Part picture;

    private Collection<Car> carCollection;

    public CarController() {car = new Car();}

    public String create() throws IOException {

    /*try (   InputStream inputStream = picture.getInputStream();
            FileOutputStream outputStream = new FileOutputStream("D:" +
            File.separator + "test" + File.separator +
            picture.getSubmittedFileName())) {
        int bytesRead = 0;
        final byte[] chunck = new byte[1024];
        while ((bytesRead = inputStream.read(chunck)) != -1) {
            outputStream.write(chunck, 0, bytesRead);
        }
    }*/

    /*Path folder = Paths.get("//resources//cars//");
    String filename = FilenameUtils.getName(picture.getName()); 
    String extension = FilenameUtils.getExtension(picture.getName());
    Path file = Files.createTempFile(folder, filename + "-", "." + extension);
    try (InputStream input = picture.getInputStream()) {
        Files.copy(input, file, StandardCopyOption.REPLACE_EXISTING);
    }*/
    picture.write("\\resources\\cars\\"+picture.getSubmittedFileName()+"."+picture.getContentType());
    car.setPicturePath(picture.getSubmittedFileName());
    carFacade.create(car);
    return "/index.xhtml";
}


这是例外:


Here is exception :

Caused by: java.lang.NullPointerException
    at Controller.CarController.create(CarController.java:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:289)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 41 more


感谢您提供任何有用的想法.


Thank you for any idea that may help.

此错误的原因是:picture.write("\ resources \ cars \" + picture.getSubmittedFileName()+." + picture.getC‌ontentType());

THIS is responsible of the error : picture.write("\resources\cars\"+picture.getSubmittedFileName()+"."+picture.getC‌​ontentType());

我尝试将其添加到web.xml中,但没有成功:

I've tried to add this to web.xml without any success :

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <multipart-config>
    <location>\resources\cars\</location>
    <max-file-size>1300000</max-file-size>
    <max-request-size>20000000</max-request-size>
    <file-size-threshold>50000</file-size-threshold>
    </multipart-config>
</servlet>

推荐答案

删除MyFacesExtensionsFilter.仅当您使用<t:inputFileUpload>而不是<h:inputFile>时才需要.

Remove the MyFacesExtensionsFilter. That's only necessary when you're using <t:inputFileUpload> instead of <h:inputFile>.

这篇关于JSF 2.2-文件上传返回java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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