1

i am now trying to use primefaces instead of a home-brewed file upload system, only problem i am having is it seems not to do anything, any ideas why ?

i am using glassfish, and i have added

commons-io-1.4.jar commons-fileupload-1.2.1/jar

to my libraries in netbeans

heres my xhtml

<p:fileUpload fileUploadListener="#{fileUploadController.upload}"
                                  mode="advanced" 
                                  update="messages"
                                  sizeLimit="100000000" 
                                  allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>

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

web.xml,

          <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:\Users\Richard\printing~subversion\fileupload\web\Uploaded</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

FileUploadController.java

    @ManagedBean(name="fileUploadController")
public class FileUploadController {
   private String destination="C:/Users/Richard/printing~subversion/fileupload/web/Uploaded";

    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());
                }
    }
}

i have also tried this example : http://e-blog-java.blogspot.co.uk/2010/04/ppr-multi-file-upload-with-primefaces.html

but the issue is when i copy the code i can not get the upload to display ?

anyone got any ideas whats going on ?

Thanks

user1924104
  • 891
  • 2
  • 16
  • 38

3 Answers3

2

Have you included the necessary imports in your project? If I remember correctly I once used primefaces upload and I needed to include a specific maven dependency for that upload thing.

After a quick google:

Commons IO
Commons File Upload

Or maven style:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2.1</version>
</dependency>

Combined with the answer above :)

fileUploadListener="#{uploaderBB.handleFileUpload}"

Resource: Resource

darkownage
  • 938
  • 16
  • 38
  • Thanks, i have added the dependences into my libraries, i am using glassfish so maven is no good :( – user1924104 Jan 28 '13 at 13:27
  • @user1924104 maven is a software that helps you to build your components, this answer just try to tell you the libraries you should add, but you have greater versions of those libraries in your project. – Luiggi Mendoza Jan 28 '13 at 13:34
  • Ah i see thank you, yes all libraries are up to date and added – user1924104 Jan 28 '13 at 13:39
2

OK solved the issue !, with primefaces you must put the filter first, before any other filters you might have ( i did have a filter from a previous file upload) so now my web.xml looks like :

<!-- <filter>
            <filter-name>Upload Filter</filter-name>
            <filter-class>richard.fileupload.UploadFilter</filter-class>
            <init-param>
                <param-name>sizeThreshold</param-name>
                <param-value>1024</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>Upload Filter</filter-name>
            <url-pattern>/upload/*</url-pattern>
        </filter-mapping> -->
        <context-param>
            <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
            <param-value>server</param-value>
        </context-param>
        <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>20480</param-value>
            </init-param>
            <init-param>
                <param-name>uploadDirectory</param-name>
                <param-value>C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>PrimeFaces FileUpload Filter</filter-name>
            <servlet-name>Faces Servlet</servlet-name>
        </filter-mapping>
        <servlet>
            <servlet-name>Faces Servlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>Faces Servlet</servlet-name>
            <url-pattern>*.xhtml</url-pattern>
        </servlet-mapping>
        <welcome-file-list>
            <welcome-file>/GUI/index.xhtml</welcome-file>
        </welcome-file-list>
        <context-param>
            <param-name>javax.faces.PROJECT_STAGE</param-name>
            <param-value>Development</param-value>
        </context-param>
        <context-param>
            <param-name>facelets.LIBRARIES</param-name>
            <param-value>/WEB-INF/corejsf.taglib.xml</param-value>
        </context-param>
        <context-param>
            <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
            <param-value>true</param-value>
        </context-param>

only thing i have noticed is now they are uploaded as a TMP file, can i still use this file for example to print out later on ?

user1924104
  • 891
  • 2
  • 16
  • 38
  • The temp files are not necessarily for your use, but for primefaces'. You're expected to retrieve the binary contents in the `FileUploadListener` and do what you want with it. – kolossus Jan 28 '13 at 20:22
-1

EDIT

This question was from January 2013 when there was only PrimeFaces 3.5. For 3.5, the default was commons-fileupload. So for 3.5, the context-param below is not required.

If you are using PrimeFaces 4 and have this problem:

The default for PrimeFaces 4 is 'native'. If you want to use commons-fileupload you need to include this parameter in your web.xml:

<context-param>
    <param-name>primefaces.UPLOADER</param-name>
    <param-value>commons</param-value>
</context-param>

With WebLogic I was using the default but when attempting to upload, nothing happened. There was no error. With Tomcat it was fine either way. I have only tried it with Tomcat ant WebLogic.

awilkinson
  • 1,601
  • 15
  • 23
  • How is this an answer to the question above? – Kukeltje Sep 18 '15 at 17:25
  • @Kukeltje Edited my answer for clarity. The question specifically mentions using commons-fileupload. A complete web.xml was not provided so it's not clear whether this context-param was included. I needed that parameter to make it work for me. This is an answer to the question above. Please remove your downvote if it was yours. It would be better to ask for clarification first and then downvote if the answer is still not good. – awilkinson Sep 18 '15 at 17:41
  • Hmmm that is your opinion. If I do not downvote, very often the incentive to improve the answer is not there, so nothing happens and I need to keep track of which answers to downvote afterwards if nothing improved in a week? Two weeks? If I downvote and someone improves the question the person often makes a comment to and I get a notice. I can then decide to remove the downvote or not. You may not like this way but it is what works best for me (and I very often do comment, like now when I downvote) – Kukeltje Sep 18 '15 at 18:13
  • And now on-topic. The question was from jan 2013 when there just was a PrimeFaces 3.5. The default for uploading was... commons, so there was no need to configure that. Following this, your assumption is wrong and hence your answer is only 'valid' if someone has the same problem but with a newer PF version (>= 4.0). Still not even then. It will fallback to 'native' and either work or result in different errors. So the answer is most likely even then not valid. If you add all this info to the answer, I'll remove the downvote – Kukeltje Sep 18 '15 at 18:19
  • btw, notice I did not downvote the other similar 'answers' by you. They were more specific – Kukeltje Sep 18 '15 at 18:26
  • @Kukeltje Ok. Point taken re: your downvote system. – awilkinson Sep 18 '15 at 18:26
  • @Kukeltje Updated with the info you mentioned. Re: your comment "Still not even then. It will fallback to 'native' and either work or result in different errors.", note that the problem I had was that with 'native' there were never any errors. Simply, nothing happened. I had to switch to commons to make it work. – awilkinson Sep 18 '15 at 18:50