1

I read many pages but, i'm sure, I did some errors somewhere and I can't do the upload of a file simply as It should be.

First, I'm developing using JSF2.0, Primefaces 3.2 and JPA2 on Glassfish 3.1 with Netbeans 7 and JDK 1.7.

The next thing that I must say is that I'm tringg to insert this code into an interface that will be extended from many others classes and that should store the files in many folders on filesystem! So now I will report what I wrote and, please, tell me where is the problem!

This is the code into web.xml:

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    <init-param>
        <param-name>uploadDirectory</param-name>
        <param-value>D:/Cyborg/</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>  

The page that contains the p:uploadFile contains also many other form's fields and I will copy only what I think is of your interest:

...
<h:form method="POST">
    ...
    <h:outputLabel for="uploadFile" value="#{bundle.LabelUploadFile}" />
    <p:fileUpload value="#{progettiController.uploadFile}" mode="simple" />
    ...
    <h:commandLink action="#{progettiController.create}" value="#{bundle.SaveLink}" />
    ...

this is the code into the interface called AbstractController:

protected UploadedFile uploadFile;

public void setUploadFile(UploadedFile uploadFile) {
    this.uploadFile=uploadFile;
}

public UploadedFile getUploadFile() {
    return uploadFile;
}

and finally the method Create into ProgettiController:

public String create() {
    try {
        getFacade().create(current);
        System.out.println("Message: ProgettiController: Create: new row created successfully!");

        try {
            current=getFacade().findLast();
            String ext=uploadFile.getFileName();
            System.out.println("Message: ProgettiController: Create: FileName="+ext);

            ext=ext.substring(ext.lastIndexOf("."),ext.length());
            System.out.println("Messaggio: ProgettiController: ext="+ext);

            current.setNomeFile(ext.substring(0,ext.lastIndexOf(".")));
            current.setTipoFile(ext.substring(ext.lastIndexOf(".")+1,ext.length()));
            getFacade().edit(current);
            ext=urlFilesystem+current.getId()+ext.substring(ext.lastIndexOf(".")+1,ext.length());
            System.out.println("Message: ProgettiController: Create: posizione e nome del file="+ext);

            File oldFile= (File)uploadFile;
            File newFile= new File(ext);
            oldFile.renameTo(newFile);
        } catch (Exception e) {
            System.out.println("Messaggio: ProgettiController: Create: errore nel try legato al upload="+e.getMessage());
            e.printStackTrace();
        }

        JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("ProgettiCreated"));
        recreateModel();
        System.out.println("Messaggio: ProgettiController: create try");
        return url+"List?faces-redirect=true";
    } catch (Exception e) {
        JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        System.out.println("Messaggio: ProgettiController: create catch="+e.getMessage());
        e.printStackTrace();
        return url+"List?faces-redirect=true";
    }
}

If I try this code the problem is that when the page call the method returns null into uploadFile, if I add the "enctype="multipart/form-data"" into h:form tag the application didn't call the method! Can someone help me? Thanks a lot!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Filippo1980
  • 2,745
  • 5
  • 30
  • 44
  • did you include the commons-io and commons-fileupload jars. These are required. – rags Apr 18 '12 at 11:30
  • Yes I did... but do you have some method for check it? I created a new library into netbeans and add it into my project... – Filippo1980 Apr 18 '12 at 12:29
  • I tried to set a new class using the auto complete option and after org.apache.commons it suggest fileupload and io, could I consider it a good thing about the implementation of commons libraries? – Filippo1980 Apr 18 '12 at 12:37
  • You do not need `
    ` tags to format code in your question. You do not need to change all `<` by `<` and `>` by `>`. That's too much work. Just indent all code with 4 spaces to get it formatted as code. Or select a block of code and press Ctrl+K in the message editor to toggle the 4-space indent. See also http://stackoverflow.com/editing-help#code
    – BalusC Apr 18 '12 at 13:12
  • @BalusC : thanks, I don't remeber where but I read that the tags
     and  were appreciate and, in the past, when I tried to write a question without replacing "<" and ">" it was not readable correctly... did stackoverflow change something in editor? :)
    – Filippo1980 Apr 18 '12 at 14:34
  • The point is to indent it with 4 spaces to get the editor to interpret it as code. Otherwise it will be interpreted as HTML (the editor supports plain HTML). This way all unknown HTML tags like `` will be ignored and not appear in the result. Click "edit" link in your question to see how I have fixed it. – BalusC Apr 18 '12 at 14:36

2 Answers2

2

if I add the "enctype="multipart/form-data"" into h:form tag the application didn't call the method!

That would mean that the PrimeFaces file upload filter wasn't able to do its job properly. It is responsible for parsing a multipart/form-data request and providing the parsed data further to JSF in an understandable form (until the upcoming JSF 2.2 version, JSF does by default not support multipart/form-data requests).

That can have at least the following main reasons:

  • The <servlet-name> in the <filter-mapping> is incorrect. You need to make sure that it's exactly the same <servlet-name> of the <servlet> configuration of the FacesServlet in the same web.xml.

  • The Commons IO and FileUpload JARs are not in the webapp's runtime classpath. I don't use Netbeans, so I can't go in detail, but whatever way you use to include the JARs in the webapp, you need to make absolutely sure that they end up in /WEB-INF/lib folder of the deployed webapp. You can verify that by checking the deploy in the server. In Eclipse, that can among others be achieved by just dropping those JARs straight in exactly that folder of the web project.

  • You happen to have another Filter in your web application which does roughly the same job as the PrimeFaces file upload filter and is executed before the PrimeFaces file upload filter. For example, the MyFaces ExtensionsFilter which is bundled in Tomahawk. The request body can namely be parsed only once.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • For the first probably reason: could the problem is the order of these parameters? I have before the filter and than the servlet...; for the last probably reason, could the problem be into the filter described on your answer here: http://stackoverflow.com/questions/9062757/are-there-some-issue-at-inserting-some-check-into-template – Filippo1980 Apr 18 '12 at 14:49
  • The order of the servlet-filter declarations is not relevant. However if you have multiple filters, then the order of the individual filter declarations may be relevant, because all filters will be invoked in the order of their `` in `web.xml`. I do not know which filters you all have in your webapp, but you could always try putting the mapping of the file upload filter as the first one of all filter mappings. At least, assuming that you didn't modify the code of the other filter, it doesn't parse the request body at all. – BalusC Apr 18 '12 at 14:55
  • I retreive the RequestURI from the other servlet... could it be the problem? – Filippo1980 Apr 18 '12 at 15:34
  • I do not understand what you mean. What do you mean with "I retrieve the RequestURI from the other servlet"? What exactly have you done and how exactly is this related to the problem? – BalusC Apr 18 '12 at 15:39
  • I have this code: String uri=request.getRequestURI(); where "request is an HttpServletRequest! Could it be the problem? If – Filippo1980 Apr 19 '12 at 07:56
  • Oh, that should not be a problem. The URI is not part of the request body. – BalusC Apr 19 '12 at 10:39
  • ok, so the problem isn't into the servlet... I just explore my war file and I found that there were not the apache jar files into WEB-INF/lib... so I add these files manually but, now, I don't know how I could see if they are deployed because I found the war file only into my project and not into Glassfish's folders! However I tried again and I received the same problem! – Filippo1980 Apr 19 '12 at 14:16
  • Where exactly did you add these files? It must basically go in `/WEB-INF/lib` of the project so that the IDE will take it rightly in the build. Again, I don't use Netbeans, but I've googled somewhat and it seems that a Netbeans web project should really already have a `/WEB-INF/lib` folder: http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/ Check the section which starts with *"Now you should have a view in your Filesystem explorer like this:"*. – BalusC Apr 19 '12 at 14:21
  • YES!!! Now the application enter into the method! Now the problem is another but for it, perhaps, I will a new question! Thanks BalusC as usual! – Filippo1980 Apr 19 '12 at 15:05
1

there's an error in your code causing the null pointer

        {..
        ext=ext.substring(ext.lastIndexOf("."),ext.length()); 
        // here ext contains the extension of the file only

        System.out.println("Messaggio: ProgettiController: ext="+ext);

        current.setNomeFile(ext.substring(0,ext.lastIndexOf(".")));
        // used the extension as the file name
        current.setTipoFile(ext.substring(ext.lastIndexOf(".")+1,ext.length()));
        // the file type is an empty string causing the error
        ..}

try the following

        {..
        ext=ext.substring(ext.lastIndexOf("."),ext.length()); 

        System.out.println("Messaggio: ProgettiController: ext="+ext);

       current.setNomeFile(uploadFile.getFileName().
                           substring(0,uploadFile.getFileName().lastIndexOf(".")));
        // the file name is setted properly
        current.setTipoFile(uploadFile.getFileName().
                           substring(uploadFile.getFileName().
                           lastIndexOf(".")+1,uploadFile.getFileName().length()));
        // the file type is setted properly
        ..}