1

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:

@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:

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. How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Clyde
  • 49
  • 2
  • 10

1 Answers1

3

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.

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. This is to be achieved by setting the enctype attribute of the form as below:

<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. Just let the action method access it directly.

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

Also, the attempt to save the uploaded file in IDE project folder is a bad idea. Don't do that. Store it elsewhere. See also a.o. Uploaded image only available after refreshing the page.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    @BalusC this works for me when mi app is running on Glassfish 4.xxx/Payara 4.xxx, now I migrated my app to Payara 5.183 and UploadedFile variable always is null, I use PrimeFaces 6.1. Have you an idea? – AngelAvila Oct 26 '18 at 20:33