Fileupload glassfish error

UI Components for JSF
Post Reply
garyyip
Posts: 67
Joined: 25 Aug 2010, 18:55

29 Aug 2010, 17:43

Hi

I trying out the FileUpload example. However i meet with this problem when i inserted the code to the web.xml

Code: Select all

    <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>/</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

Below is the glassfish error


descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: PWC1243: Filter execution threw an exception
root cause

java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
root cause

java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory


How can i resolve this?
Many thank

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

30 Aug 2010, 01:11

You need to add commons-fileupload, see dependencies section in user's guide.

garyyip
Posts: 67
Joined: 25 Aug 2010, 18:55

30 Aug 2010, 10:35

Thks alot... i have solve it by adding the dependancy.

Beside that I still unsure how to upload/save the file to a specify directory. At the web xml do you need to specify the directory

<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>E:/Software</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>


I want to upload the file to path: E:/Software.... how can i do it?
Beside putting the directory <<E:/Software>> at the param-value, is there extra coding to add to the backing bean in setting the specify directory?

Thks

User avatar
mouadh
Posts: 85
Joined: 27 Apr 2010, 12:47
Location: Tunisia

30 Aug 2010, 11:17

UploadDirectory is used to temporary store the files that exceeds the thresholdSize, and doesn't meant to be the directory that the uploaded files are stored to. you can do that by getting the file from the FileUploadListner and then you can store it by implementing your own code.
Ben Khalifa Mouadh
engineering student at National School of Computer Sciences - Tunisia
JSF 2.0, GlassFish v3, PF 2.0.1,Majorra 2.0.2, netBeans 6.8

garyyip
Posts: 67
Joined: 25 Aug 2010, 18:55

30 Aug 2010, 17:33

thank for your reply

So do i still leave web xml to be

<init-param>
<param-name>uploadDirectory</param-name>
<param-value>E:/Software</param-value>
</init-param>


or use the default value for the param-value?

Can you provided a sample code or any ref link for the FileUploadListner to save in my specify directories.

Many thanks

marco
Posts: 66
Joined: 14 Jun 2010, 17:25

30 Aug 2010, 17:39

the parameter in web.xml is NOT a solution for your requirement!

You have to implement it by yourself how and where to write the uploaded files! The context parameter is only used for temporary files and your filelistener method gets a stream object for this temp file. PF upload even works without the context param, as it then will use standard java temp folder.

An example how to implement a filelistener method is in the PF documentation. In this method you have to write the uploaded file somewhere in the file system.

garyyip
Posts: 67
Joined: 25 Aug 2010, 18:55

30 Aug 2010, 19:06

I manage to upload to the desired directory. But it is not a image file such as .jpg or.png . In fact 9 .tmp is created. How to make it to image format?

Code: Select all

 public void handleFileUpload(FileUploadEvent event) {
       UploadedFile uf = event.getFile();
        String filename = uf.getFileName();
        File f = new File("E:\\Software" + filename);
        OutputStream os = null;
        InputStream is = null;
        try {
            is = uf.getInputstream();
            byte[] b = new byte[is.available()];
            os = new FileOutputStream(f);
            while (is.read(b) > 0) {
                os.write(b);
            }
            FacesMessage msg = new FacesMessage("Upload Successful", event.getFile().getFileName());
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (IOException ex) {

        } finally {
            try {
                os.flush();
                os.close();
                is.close();
            } catch (IOException ex) {

            }
        }

artishx
Posts: 2
Joined: 13 Sep 2010, 13:25

13 Sep 2010, 13:30

Hi, i'm having the same problem, the files are saved as .tmp but i'm unable to do a .renameTo(); or copy the files over to the desired folder.

any insights?

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 19 guests