5

I have a p:fileUpload function on my page and every time I upload a file I cannot seem to find it in the folder specified in my web.xml file.

I have added the following jars to my library: primefaces-3.2.jar commons-io-2.3.jar commons-fileupload-1.2.2.jar

Here is my web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0">

    <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>C:\Users\SomeUser\Documents\NetBeansProjects\System\Upload\</param-value> 
    </init-param> 
    <init-param> 
        <param-name>thresholdSize</param-name> 
        <param-value>1000000</param-value> 
    </init-param> 
    </filter>

    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

    <error-page>
        <error-code>404</error-code>
        <location>/404.jsf</location>
    </error-page>

    <context-param>  
    <param-name>primefaces.THEME</param-name>  
    <param-value>aristo</param-value>  
</context-param>  

</web-app>

I am using the following: PrimeFaces 3.2, JSF 2.0 and GlassFish 3.1.1

Any help will be appreciated. Thanks

Dani
  • 3,744
  • 4
  • 27
  • 35
user1404693
  • 65
  • 1
  • 2
  • 7

1 Answers1

9

It stores it by default in the system default temp directory as identified by java.io.tmpdir system property. You could use UploadedFile#getContents() or UploadedFile#getInputStream() to get the file contents and write it to the desired folder. But you can also change the default upload location by an initialization parameter of the filter.

Put this inside the <filter> element of the file upload filter:

<init-param>
    <param-name>uploadDirectory</param-name>
    <param-value>/path/to/uploads</param-value>
</init-param>

Note that when you're running Windows, it will be relative to the disk from where the webserver is started. So if it's C:\, then the above init param will actually resolve to C:\path\to\uploads. You should also make sure that this folder is already been prepared beforehand and thus exists and is writable.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Thanks again BalusC, it now uploads to correct folder however it is a temporary file, not sure how to handle this. – user1404693 May 19 '12 at 15:37
  • It's indeed intended to be a temporary file. The normal approach is to get its contents and then write it to the desired location with the desired filename by `FileOutputStream` in the bean's action method. – BalusC May 19 '12 at 16:10
  • If the uploader is set to auto (native cuz jsf >= 2.2) how should I change the file location ? primefaces.UPLOADER auto – Ced Jun 09 '15 at 12:23