简体   繁体   English

当使用以下命令上传文件时,UploadedFile为null <p:fileUpload mode=“simple”>

[英]UploadedFile is null when uploading file with <p:fileUpload mode=“simple”>

I need to upload an image into the server. 我需要将图像上传到服务器。 I'm using primefaces, here is my code: 我正在使用素数,这是我的代码:

deposit.xhtml

<h:form>
    <p:fileUpload mode="simple"
        allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
        value="#{imageHandler.uploadedPicture}" />

    <p:commandButton action="#{imageHandler.savefile(imageHandler.uploadedPicture)}" 
        value="Déposer" ajax="false" />
</h:form>

ImageHandler : ImageHandler

@ManagedBean (name = "imageHandler")
@RequestScoped
public class ImageHandler {

    private UploadedFile uploadedPicture; // +getter+setter

    public void savefile(UploadedFile uploadedPicture)
    {
        try {
            InputStream input = uploadedPicture.getInputstream();
            File folder = new File("C:\\Users\\Clyde\\Documents\\NetBeansProjects\\DSJEE\\web\\resources\\Items");
            String filename = FilenameUtils.getBaseName(uploadedPicture.getFileName()); 
            String extension = FilenameUtils.getExtension(uploadedPicture.getFileName());
            File file = File.createTempFile(filename + "-", "." + extension, folder);
            Files.copy(input, file.toPath());
            FacesContext.getCurrentInstance().getExternalContext().redirect("index2.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(ImageHandler.class.getName()).log(Level.SEVERE, null, ex);
        }      
    }
}

Concerning the trace here is the error I get, those are 3 lines that I have picked from the trace: 关于跟踪,这里是我得到的错误,这是我从跟踪中选择的3行:

javax.faces.el.EvaluationException: java.lang.NullPointerException
Caused by: java.lang.NullPointerException
at ImageHandler.savefile(ImageHandler.java:43)

In other words, it's coming from here: 换句话说,它来自这里:

InputStream input = uploadedPicture.getInputstream();

I have tried many things to get rid of that error. 我已经尝试了很多方法来消除该错误。 I used savefile() without parameters, changed the scope, etc... Still can't go on. 我使用了不带参数的savefile() ,更改了范围,等等。。。仍然无法继续。 How is this caused and how can I solve it? 这是怎么引起的,我该如何解决?

It will be null in the action method when the browser is unable to send the file contents along with the request body, or when the server is unable to grab the file contents from the request body. 当浏览器无法将文件内容与请求正文一起发送时,或者服务器无法从请求正文中获取文件内容时,在action方法中它将为null

In order to let the broswer send the file contents (and thus not only the name), you need to make sure that the request body encoding type is set to multipart/form-data . 为了让浏览器发送文件内容(不仅是名称),您需要确保将请求正文编码类型设置为multipart/form-data This is to be achieved by setting the enctype attribute of the form as below: 这可以通过如下设置表单的enctype属性来实现:

<h:form enctype="multipart/form-data">

Unrelated to the concrete problem, the below doesn't make sense: 具体问题无关 ,以下内容没有任何意义:

<p:commandButton action="#{imageHandler.savefile(imageHandler.uploadedPicture)}">

You don't need to pass a bean property forth and back to the very same bean. 您不需要传递一个bean属性并将其传递回相同的bean。 Just let the action method access it directly. 只需让action方法直接访问它即可。

<p:commandButton action="#{imageHandler.savefile}">

Also, the attempt to save the uploaded file in IDE project folder is a bad idea. 另外,尝试将上传的文件保存在IDE项目文件夹中也是一个坏主意。 Don't do that. 不要那样做 Store it elsewhere. 将其存储在其他位置。 See also ao Uploaded image only available after refreshing the page . 另请参见ao 上传的图像仅在刷新页面后可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM